JPA一对一配置是在实体之间建立一对一的关系,它可以让我们更好地把握数据库中的数据,使用JPA来配置一对一关系也是很常见的。但是,在使用JPA配置一对一关系时,也会遇到很多坑。
首先,要注意两个实体之间的映射关系。如果不正确地设置映射关系,可能会出现“No identifier specified for entity”的异常。因此,我们必须正确地设置实体之间的映射关系,才能避免这个异常。
另外,要注意实体之间的字段名是否相同。如果字段名不相同,可能会出现“org.hibernate.MappingException: Repeated column in mapping for entity”异常。因此,我们必须保证实体之间字段名相同才能避免这个异常。
最后要注意@JoinColumn注解中name属性和referencedColumnName属性是否正确赋值。如果不正确赋值,可能会出现“org.hibernate.AnnotationException: referencedColumnNames(xxx) of xxx.xxx referencing xxx not mapped to a single property”异常。因此,我们必须保证@JoinColumn注解中name属性和referencedColumnName属性都正确赋值才能避免这个异常。
@Entity public class Person { @Id @GeneratedValue private Long id; private String name; @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "address_id", referencedColumnName = "id") // 此处要注意name 和 referencedColumnName 的赋值 private Address address; // getter and setter ... } @Entity public class Address { @Id @GeneratedValue private Long id; private String detail; // getter and setter ... }
以下代码显示如何使用延迟加载设置执行一对一映射。
@OneToOne(fetch=LAZY) private Department department;
下面的代码来自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; import static javax.persistence.FetchType.LAZY; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne(fetch=LAZY) 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; }下载 OneToOne_Lazy_Load.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 多对多连接表示例以下代码显示如何为多对多映射设置连接表。例子下面的代码来自PersonDaoImpl.java。package cn..c...
JPA教程 -JPA查询连接多对一示例以下JPQL显示如何连接多对一映射。SELECT p FROM Professor e JOIN e.phones p)例子下面的代码来...
JPA教程 -JPA查询左连接示例以下代码显示了如何在JPQL中使用左连接。List l = em.createQuery(SELECT e, dFROM Professor e LEFT...
JPA教程 -JPA查询函数示例JPQL支持的函数表达式如下所示。函数描述ABS(number)返回number参数的无符号版本。结果类型与参数类型(...
JPA教程 -JPA查询编号索引参数示例以下代码显示了如何在JPQL中使用数字索引参数。Collection expired = em.createQuery(SELECT u...