JPA 一对多映射是 JPA 中最常用的映射方式,它表示一个实体对象可以关联到多个其他实体对象。在 JPA 中,一对多映射可以通过 @OneToMany 注释来实现,该注释可以应用于实体类的属性上,表示该属性是一个集合,其中包含了多个关联的实体。
要正确配置一对多映射,需要在拥有 @OneToMany 注释的属性上添加 @ManyToOne 注释。@ManyToOne 注释表示该属性是一个引用,指向另一个实体。这样,JPA 将能够正确地将这些实体之间的关联建立起来。
@Entity public class Employee { @Id private Long id; private String name; @OneToMany(mappedBy = "employee") private List<Task> tasks; // getters and setters... }
@Entity public class Task { @Id private Long id; private String description; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "employee_id") private Employee employee; // getters and setters... }
以下代码显示了如何进行单向一对多映射。
下面的代码来自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); Phone p = new Phone(); p.setNumber("1234509876");; emp.addPhone(p); em.persist(p); em.persist(emp); } @PersistenceContext private EntityManager em; }
以下代码来自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.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.OneToMany; @Entity public class Professor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @OneToMany @JoinTable(name="EMP_PHONE", joinColumns=@JoinColumn(name="EMP_ID"), inverseJoinColumns=@JoinColumn(name="PHONE_ID")) private Collection<Phone> phones; public Professor() { phones = new ArrayList<Phone>(); } 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 addPhone(Phone phone) { if (!getPhones().contains(phone)) { getPhones().add(phone); } } public Collection<Phone> getPhones() { return phones; } public String toString() { return "Employee id: " + getId() + " name: " + getName() + " with " + getPhones().size() + " phones"; } }
以下代码来自Phone.java。
package cn..common; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Phone { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Column(name="NUM") private String number; private String type; public int getId() { return id; } public void setId(int 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 String toString() { return "Phone id: " + getId() + ", no: " + getNumber() + ", type: " + getType(); } }下载 OneToMany_Unidirectional.zip
以下是数据库转储。
Table Name: EMP_PHONE Row: Column Name: EMP_ID, Column Type: INTEGER: Column Value: 1 Column Name: PHONE_ID, Column Type: INTEGER: Column Value: 1 Table Name: PHONE Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NUM, Column Type: VARCHAR: Column Value: 1234509876 Column Name: TYPE, Column Type: VARCHAR: Column Value: null 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
JPA教程 -JPA命名查询参数类型示例在命名查询中,我们还可以添加参数。@Entity@NamedQueries({@NamedQuery(name=findEmployeesAb...
JPA教程 -JPA查询简介示例最简单的JPQL查询选择单个实体类型的所有实例。考虑下面的查询:SELECT eFROM Employee eJPQL尽可能使用...
JPA教程 -JPA 查询计数其中子查询示例以下代码显示如何在子查询中使用COUNT函数。List l = em.createQuery(SELECT m FROM Profes...
JPA教程 - JPA 查询Exists示例如果子查询返回任何行,则EXISTS条件返回true。以下代码显示如何在JPQL中使用带有子查询的EXISTS运...
JPA教程 -JPA查询日期参数示例我们可以在查询中使用日期类型值。以下代码使用EntityManager创建具有两个参数的查询。然后它传递...