JPA 一对多映射是 JPA 中最常用的映射方式,它表示一个实体对象可以关联到多个其他实体对象。在 JPA 中,一对多映射可以通过 @OneToMany 注释来实现。
@OneToMany 注释由三个属性组成,分别是 targetEntity、mappedBy 和 cascade。targetEntity 属性指定了要关联的目标实体;mappedBy 属性指定了在目标实体中的哪个字段表示这种关联;cascade 属性指定了当更新一方时,另一方也会被更新的情况。
@Entity public class Department { @Id private Long id; private String name; @OneToMany(targetEntity = Employee.class, mappedBy = "department", cascade = CascadeType.ALL) private List<Employee> employees; // getters and setters... }
@Entity public class Employee { @Id private Long id; private String name; @ManyToOne(targetEntity = Department.class) private Department department; // getters and setters... }
以下代码显示如何创建双向多对多映射。
以下代码来自Professor.java。
package cn..common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Professor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @ManyToMany private Collection<Project> projects; public Professor() { projects = new ArrayList<Project>(); } 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 long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public void addProject(Project project) { if (!getProjects().contains(project)) { getProjects().add(project); } if (!project.getEmployees().contains(this)) { project.getEmployees().add(this); } } public Collection<Project> getProjects() { return projects; } public String toString() { return "Employee id: " + getId() + " name: " + getName() + " with " + getProjects().size() + " projects"; } }
下面的代码来自PersonDaoImpl.java。
package cn..common; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Professor emp = new Professor(); emp.setName("name"); emp.setSalary(12345); Project p = new Project(); p.setName("Chem"); p.getEmployees().add(emp); emp.getProjects().add(p); em.persist(p); em.persist(emp); } @PersistenceContext private EntityManager em; }
以下代码来自Project.java。
package cn..common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Project { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) protected int id; protected String name; @ManyToMany(mappedBy="projects") private Collection<Professor> employees; public Project() { employees = new ArrayList<Professor>(); } public int getId() { return id; } public void setId(int projectNo) { this.id = projectNo; } public String getName() { return name; } public void setName(String projectName) { this.name = projectName; } public Collection<Professor> getEmployees() { return employees; } public void addEmployee(Professor employee) { if (!getEmployees().contains(employee)) { getEmployees().add(employee); } if (!employee.getProjects().contains(this)) { employee.getProjects().add(this); } } public String toString() { return "Project id: " + getId() + ", name: " + getName() + " with " + getEmployees().size() + " employees"; } }下载 ManyToMany_Bidirectional.zip
以下是数据库转储。
Table Name: PROFESSOR Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: name Column Name: SALARY, Column Type: BIGINT: Column Value: 12345 Table Name: PROFESSOR_PROJECT Row: Column Name: EMPLOYEES_ID, Column Type: INTEGER: Column Value: 1 Column Name: PROJECTS_ID, Column Type: INTEGER: Column Value: 1 Table Name: PROJECT Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: Chem
JPA教程 -JPA多对一连接列示例以下代码标记了在多对一映射中使用的连接列。@ManyToOne (cascade=CascadeType.ALL)@JoinColumn(na...
JPA教程 -JPA查询检索多对一映射示例以下查询从多对一映射检索实体。List l = em.createQuery(SELECT d.employees FROM Departme...
JPA教程 -JPA查询连接三个实体示例以下JPQL显示如何连接三个实体。List l = em.createQuery( SELECT DISTINCT p FROM Department...
JPA教程 -JPA查询选择两个属性示例从别名开始,我们可以使用点(.)运算符浏览实体关系。以下JPQL从一个实体中选择两个属性。Lis...