JPA一对一关联查询是指在两个实体之间建立一对一的关联,这种关联可以用来表示两个实体之间的父子关系,也可以用来表示多对一的关系。JPA中的一对一关联查询可以使用@OneToOne注释来实现,这样就可以在实体之间建立一对一的关联。
JPA中的一对一关联查询有两种方式:单向和双向。单向是指在实体A中有@OneToOne注释,而在实体B中没有@OneToOne注释;双向是指在实体A和实体B中都有@OneToOne注释。
// 单向 @Entity public class A { @Id private Long id; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "b_id") private B b; // getter and setter ... } // 双向 @Entity public class A { @Id private Long id; @OneToOne(mappedBy = "a", fetch = FetchType.LAZY) private B b; // getter and setter ... } @Entity public class B { @Id private Long id; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "a_id") // 此处需要加上JoinColumn注释,表明此处是外键列 // 此处需要加上JoinColumn注释,表明此处是外键列 // 此处需要加上JoinColumn注释,表明此处是外键列 // 此处需要加上JoinColumn注释,表明此处是外键列 // 此处需要加上JoinColumn注释,表明此处是外键列 // 此处需要加上JoinColumn注释,JPA 一对一连接列示例
JPA教程 - JPA一对一连接列示例
以下部分显示如何在JPA中为一对一映射设置连接列。
我们使用
@JoinColumn
注释设置连接列。@OneToOne @JoinColumn(name="DEPT_ID") private Department department;例子
下面的代码来自Department.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 Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne(mappedBy="department") private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } 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 + ", person=" + person + "]"; } }下面的代码来自Person.java。
package cn..common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne @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(){ Person p1 = new Person("Tom"); p1.setName("Tom"); Department d = new Department(); d.setName("Design"); p1.setDepartment(d); d.setPerson(p1); em.persist(p1); em.persist(d); Query query = em.createQuery("SELECT e FROM Person e"); List<Person> list = (List<Person>) query.getResultList(); System.out.println(list.get(0).getDepartment()); } @PersistenceContext private EntityManager em; }下载 OneToOne_Map_Retrieve.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
JPA教程 -JPA 多对多映射示例以下代码显示如何创建多对多映射。它描述了人与部门之间的关系。一个部门可以有很多人,一个人可以...
JPA教程 -JPA 单向一对多映射示例以下代码显示了如何进行单向一对多映射。例子下面的代码来自PersonDaoImpl.java。package cn..c...
JPA教程 -JPA命名查询参数类型示例在命名查询中,我们还可以添加参数。@Entity@NamedQueries({@NamedQuery(name=findEmployeesAb...
JPA教程 -JPA查询简介示例最简单的JPQL查询选择单个实体类型的所有实例。考虑下面的查询:SELECT eFROM Employee eJPQL尽可能使用...