JPA 多对一(Many-to-One)是 JPA 中最常用的关联关系之一,它表示一个实体可以关联到多个实体。在 JPA 中,多对一的关联关系由 @ManyToOne 注释来表示。
@ManyToOne 注释可以应用于实体的属性上,表示该属性是多对一的关联。例如,在下面的代码中,我们将 Department 实体与 Employee 实体进行了关联:
@Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne(fetch = FetchType.LAZY) private Department department; // Getters and setters omitted for brevity }
在上面的代码中,我们使用 @ManyToOne 来将 Employee 实体与 Department 实体进行了关联。此外,我们还添加了 fetch 属性来控制加载 Department 的方式。在这里我们使用 FetchType.LAZY 来懒加载 Department 实体。
此外,@ManyToOne 还有一些可选属性可以使用。例如 cascade 属性可以控制当更新 Employee 时是否会同时更新 Department 等等。
最后要注意的是,JPA 多对一的关联必须要有一端作为 owner 端来担当维护端的角色。也就是说必须要有一端来保存外键信息。在上面的例子中,Employee 是 owner 端而 Department 是 inverse 端。
以下代码显示了如何在多对一映射中设置连接列。
@ManyToOne @JoinColumns({ @JoinColumn(name="DEPT_NAME", referencedColumnName="Name"), @JoinColumn(name="DEPT_ID", referencedColumnName="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 implements java.io.Serializable{ @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.JoinColumns; import javax.persistence.ManyToOne; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToOne @JoinColumns({ @JoinColumn(name="DEPT_NAME", referencedColumnName="Name"), @JoinColumn(name="DEPT_ID", referencedColumnName="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_Two_Join_Columns.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_NAME, Column Type: VARCHAR: Column Value: Design 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_NAME, Column Type: VARCHAR: Column Value: Design Column Name: DEPT_ID, Column Type: BIGINT: Column Value: 1
JPA教程 -JPA表名示例默认情况下,JPA使用类的简单名称来命名相应的表。@Entity@Table(name="EMP")public class Person {例子下...
Java 实例 - 文件写入 Java 实例以下实例演示了使用 write() 方法向文件写入内容:import java.io.*;public class Main { public...
Java 实例 - 在指定目录中创建文件 Java 实例以下实例演示了使用 File 类的file.createTempFile() 方法在指定目录中创建文件:im...
Java设计模式 - 原型模式原型模式是创建模式之一。原型模式有助于创建具有更好性能的重复对象。在原型模式中,将返回一个现有对...