JPA 一对多映射是 JPA 中最常用的映射方式,它表示一个实体对象可以关联到多个实体对象。在 JPA 中,一对多映射可以使用 @OneToMany 注释来实现,这个注释可以放在一个实体类上,表示这个实体类可以关联到多个其他实体。
@Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @OneToMany(mappedBy = "employee") private Listprojects; // getters and setters... }
上面的代码演示了如何使用 @OneToMany 注释来创建一对多映射。在这里,我们将 Employee 实体和 Project 实体之间建立了一对多的关联。Employee 实体有一个 projects 字段,该字段是一个 List 类型的集合,用于存储 Employee 关联的 Project 实例。
本节介绍如何在JPA映射中执行一对一映射。
此示例假设一个人只能是一个部门的一部分,而一个部门只能有一个人。
在Person实体中,我们使用@OneToOne注释标记Department引用属性。
@Entity public class Person { ... @OneToOne private Department department; ...
这里是设置两个实体并将它们保存到数据库的简单代码。
Person p1 = new Person("Tom"); p1.setName("Tom"); Department d = new Department(); d.setName("Design"); p1.setDepartment(d); em.persist(p1); em.persist(d);
下面的代码来自Person.java。
package cn..common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne 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 + "]"; } }
下面的代码来自Department.java。
package cn..common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; 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; } }
下面的代码来自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(){ Person p1 = new Person("Tom"); p1.setName("Tom"); Department d = new Department(); d.setName("Design"); p1.setDepartment(d); em.persist(p1); em.persist(d); } @PersistenceContext private EntityManager em; }Download OneToOne_Map.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: DEPARTMENT_ID, Column Type: BIGINT: Column Value: 1
JPA教程 -JPA双向多对多映射示例以下代码显示如何创建双向多对多映射。例子以下代码来自Professor.java。package cn..common;imp...
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...