Java教程 第59页 JPA教程 – JPA查询对象函数示例 关键字OBJECT可以用于指示查询的结果类型是绑定到标识变量的实体。 它对查询没有影响,但它可以用作可视线索。 SELECT OBJECT(d) FROM Department d 以下JPQL在JPQL中使用Object函数。 List l = em.createQuery( "SELECT OBJECT(d) FROM Department d") .getResultList(); 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Department { @Id private int id; private String name; @OneToMany(mappedBy="department") private Set<Professor> employees = new HashSet<Professor>(); public int getId() { return id; } public void setId(int deptNo) { this.id = deptNo; } public String getName() { return name; } public void setName(String deptName) { this.name = deptName; } public Set<Professor> getProfessors() { return employees; } public String toString() { return "Department no: " + getId() + ", name: " + getName(); } } 以下代码来自Project.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity;...
2024-04-01
JPA教程 – JPA查询成员示例 MEMBER OF运算符和NOT MEMBER OF是检查实体是否是集合关联路径的成员的简略方法。 以下代码显示了如何在JPQL中使用 MEMBER OF 运算符。 "SELECT e FROM Professor e WHERE e MEMBER OF e.directs") 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Department { @Id private int id; private String name; @OneToMany(mappedBy="department") private Set<Professor> employees = new HashSet<Professor>(); public int getId() { return id; } public void setId(int deptNo) { this.id = deptNo; } public String getName() { return name; } public void setName(String deptName) { this.name = deptName; } public Set<Professor> getProfessors() { return employees; } public String toString() { return "Department no: " + getId() + ", name: " + getName(); } } 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;...
2024-04-01
JPA教程 – JPA查询新对象示例 我们可以在JPQL中创建新对象。 以下代码调用JPQL中的EmployeeDetails构造函数。 executeAndPrintQuery("SELECT NEW cn.w3cschool.common.EmployeeDetails(e.name, e.salary, e.department.name) FROM Employee e"); 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.Arrays; import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee emp = new Employee(); emp.setName("Tom"); emp.setSalary(123); emp.setStartDate(new Date()); emp.setId(1); Project pro = new Project(); pro.setName("Design"); pro.getEmployees().add(emp); Department dept = new Department(); dept.setName("Dept"); dept.getEmployees().add(emp); emp.setDepartment(dept); emp.getProjects().add(pro); em.persist(dept); em.persist(pro); em.persist(emp); executeAndPrintQuery("SELECT NEW cn.w3cschool.common.EmployeeDetails(e.name, e.salary, e.department.name) FROM Employee e"); } private void executeAndPrintQuery(String queryString) { try { Query query = em.createQuery(queryString); printQueryResult(queryString, query.getResultList()); } finally { em.close(); } } private void printQueryResult(String queryString, List result) { System.out.println("EJB QL: " + queryString+"\n"); System.out.println("Result:\n"); if (result.isEmpty()) { System.out.println("No results Found\n"); } else { for...
2024-04-01
JPA教程 – JPA删除关系示例 我们可以通过将引用设置为null来删除映射中的关系,如下所示。 以下代码删除了地址和员工之间的链接,然后从数据库中删除该地址。 Employee emp = new Employee(); emp.setName("Tom"); emp.setId(1); Address addr = new Address(); addr.setId(1); addr.setStreet("street"); addr.setCity("city"); addr.setState("state"); emp.setAddress(addr); em.persist(emp); emp = em.find(Employee.class, 1); addr = emp.getAddress(); emp.setAddress(null); em.remove(addr); 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee emp = new Employee(); emp.setName("Tom"); emp.setId(1); Address addr = new Address(); addr.setId(1); addr.setStreet("street"); addr.setCity("city"); addr.setState("state"); emp.setAddress(addr); em.persist(emp); emp = em.find(Employee.class, 1); addr = emp.getAddress(); emp.setAddress(null); em.remove(addr); } @PersistenceContext private EntityManager em; } 以下代码来自Address.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Address { @Id private int id; private String street; private String city; private String state; private String zip; public int getId() { return id;...
2024-04-01
JPA教程 – JPA持久关系级联示例 以下代码显示级联持久操作。 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee emp = new Employee(); emp.setName("Tom"); emp.setId(1); Address addr = new Address(); addr.setId(1); addr.setStreet("street"); addr.setCity("city"); addr.setState("state"); emp.setAddress(addr); em.persist(emp); } @PersistenceContext private EntityManager em; } 以下代码来自Employee.java。 package cn.w3cschool.common; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Employee { @Id private int id; private String name; @ManyToOne(cascade=CascadeType.PERSIST) Address address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address...
2024-04-01
JPA教程 – JPA删除关系级联示例 以下代码显示如何删除具有级联操作的相关实体。 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee emp = new Employee(); emp.setName("Tom"); emp.setId(1); Phone ph = new Phone(); ph.setEmployee(emp); ph.setNumber("1234567890"); ParkingSpace ps = new ParkingSpace(); ps.setEmployee(emp); ps.setLocation("Dept"); emp.setParkingSpace(ps); emp.getPhones().add(ph); em.persist(emp); emp = em.find(Employee.class, 1); em.remove(emp); } @PersistenceContext private EntityManager em; } 以下代码来自Phone.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Phone { @Id private long id; private String number; private String type; @OneToOne Employee employee; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String phoneNo) { this.number = phoneNo; } public...
2024-04-01
JPA教程 – JPA查询更新示例 以下代码显示如何使用JPQL中的update语句更新表中的实体。 em.createQuery( "UPDATE Phone p SET p.number = " + "CONCAT("0",SUBSTRING(p.number," + "LOCATE(p.number, "-"), 4)), p.type = "Business" ") .executeUpdate(); 例子 以下代码来自Phone.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Phone { @Id private long id; private String number; private String type; @ManyToOne Professor employee; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String phoneNo) { this.number = phoneNo; } public String getType() { return type; } public void setType(String phoneType) { this.type = phoneType; } public Professor getProfessor() { return employee; } public void setProfessor(Professor employee) { this.employee = employee; } public String toString() { return...
2024-04-01
JPA教程 – JPA更新示例 以下代码显示了如何通过仅使用映射实体的setter方法来更新实体。 下面的代码在persist方法调用后用setter方法更新name字段。 从下一节中的数据库转储我们可以看到,新的值被保存到数据库。 Professor emp = new Professor(); emp.setId(1); emp.setName("name"); em.persist(emp); emp.setName("New name"); 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import java.util.Date; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity @Table(name="EMP") public class Professor { @Id private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "Professor id: " + getId() + " name: " + getName(); } } 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test(){ Professor emp...
2024-04-01
JPA教程 – JPA一对多非类型化映射示例 以下代码表示非类型的java.util.Map的目标实体类。 @OneToMany(targetEntity=Employee.class, mappedBy="department") @MapKey private Map employees; 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee e = new Employee(); e.setName("Tom"); Department d = new Department(); d.setName("test"); d.getEmployees().put(e, e.getId()); em.persist(e); em.persist(d); } @PersistenceContext private EntityManager em; } 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashMap; import java.util.Map; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MapKey; import javax.persistence.OneToMany; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(targetEntity=Employee.class, mappedBy="department") @MapKey private Map employees; public Department() { employees = new HashMap(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; }...
2024-04-01
JPA教程 – JPA一对多映射标记顺序列示例 以下代码显示如何在一对多映射中标记顺序列。 @OneToMany(mappedBy="queue") @OrderColumn(name="PRINT_ORDER") private List<PrintJob> jobs; 例子 以下代码来自PrintJob.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class PrintJob { @Id private int id; @ManyToOne private PrintQueue queue; public int getId() { return id; } public void setId(int id) { this.id = id; } public PrintQueue getQueue() { return queue; } public void setQueue(PrintQueue queue) { this.queue = queue; } } 以下代码来自PrintQueue.java。 package cn.w3cschool.common; import java.util.List; import java.util.ArrayList; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OrderColumn; @Entity public class PrintQueue { @Id private String name; @OneToMany(mappedBy="queue") @OrderColumn(name="PRINT_ORDER") private List<PrintJob> jobs; public PrintQueue() { jobs = new ArrayList<PrintJob>(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<PrintJob>...
2024-04-01