JPA 多对一(Many-to-One)是 JPA 中最常用的关联关系之一,它表示一个实体可以关联到多个实体。在 JPA 中,多对一的关联关系由 @ManyToOne 注释来表示。
@Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "department_id") private Department department; // getters and setters, constructors, toString()... }
上面的代码段中,Employee 实体有一个多对一的关联到 Department 实体。@ManyToOne 注释表明 Employee 和 Department 之间存在多对一的关联关系。@JoinColumn 指定了外键列名为 department_id。
以下代码标记了在多对一映射中使用的连接列。
@ManyToOne (cascade=CascadeType.ALL) @JoinColumn(name="DEPT_ID") private Department department;
下面的代码来自Department.java。
package cn..common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OrderBy; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToMany(targetEntity=Person.class, mappedBy="department") @OrderBy("name ASC") private Set<Person> persons = new HashSet<Person>(); public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Department [id=" + id + ", name=" + name + ", persons=" + persons + "]"; } }
下面的代码来自Person.java。
package cn..common; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToOne (cascade=CascadeType.ALL) @JoinColumn(name="DEPT_ID") private Department department; public Person() {} public Person(String name) { this.name = name; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + "]"; } }
下面的代码来自PersonDaoImpl.java。
package cn..common; 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(){ Department d = new Department(); d.setName("Design"); Person p1 = new Person("Tom"); p1.setDepartment(d); Person p2 = new Person("Jack"); p2.setDepartment(d); d.getPersons().add(p1); d.getPersons().add(p2); em.persist(p1); em.persist(p2); em.persist(d); } @PersistenceContext private EntityManager em; }下载 ManyToOne_Join_Column.zip
以下是数据库转储。
Table Name: DEPARTMENT Row: Column Name: ID, Column Type: BIGINT: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: Design Table Name: PERSON Row: Column Name: ID, Column Type: BIGINT: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: Tom Column Name: DEPT_ID, Column Type: BIGINT: Column Value: 1 Row: Column Name: ID, Column Type: BIGINT: Column Value: 2 Column Name: NAME, Column Type: VARCHAR: Column Value: Jack Column Name: DEPT_ID, Column Type: BIGINT: Column Value: 1
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...
JPA教程 -JPA查询AVG连接GroupBy示例以下代码显示如何在JPQL中使用连接子句使用AVG。List l = em.createQuery( SELECT d.name, A...