Scenes
Introduction to JPA and build HelloWorld (with code to download):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937
JPA to implement many-to-one-way relationship:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103511623
JPA in one-way-to-many relationship:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103520083
JPA to achieve two-way-to-many relationship:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103523564
After the realization of the above mapping relationship according to the above process, how to achieve a two-way one to one mapping in the JPA.
For example, the department manager is a two-way one to one relationship.
Note:
Blog home page:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.
achieve
In order to construct a two-way one to one relationship, new managers and department database tables and two entity classes.
New database manager table table JPA_MANAGERS
Design department table JPA_DEPARTMENTS
Then the new sector entities like Department
com.badao.jpa.helloworld package; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Table(name="JPA_DEPARTMENTS") @Entity public class Department { private Integer id; private String deptName; private Manager mgr; @GeneratedValue(strategy = GenerationType.IDENTITY) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="DEPT_NAME") public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } //使用 @OneToOne 来映射 1-1 关联关系。 //若需要在当前数据表中添加主键则需要使用 @JoinColumn 来进行映射. 注意, 1-1 关联关系, 所以需要添加 unique=true @JoinColumn(name="MGR_ID", unique=true) @OneToOne(fetch=FetchType.LAZY) public Manager getMgr() { return mgr; } public void setMgr(Manager mgr) { this.mgr = mgr; } }
注:
1.使用 @OneToOne 来映射 1-1 关联关系。
2.若需要在当前数据表中添加主键则需要使用 @JoinColumn 来进行映射. 注意, 1-1 关联关系, 所以需要添加 unique=true。
3.这里是双向的一对一映射,所以要选择一方作为维护主要关联关系的一方添加@JoinColumn(name="MGR_ID", unique=true)
然后再新建经理实体类
package com.badao.jpa.helloworld; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; @Table(name="JPA_MANAGERS") @Entity public class Manager { private Integer id; private String mgrName; private Department dept; @GeneratedValue(strategy = GenerationType.IDENTITY) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="MGR_NAME") public String getMgrName() { return mgrName; } public void setMgrName(String mgrName) { this.mgrName = mgrName; } //对于不维护关联关系, 没有外键的一方, 使用 @OneToOne 来进行映射, 建议设置 mappedBy=另一方外键 @OneToOne(mappedBy="mgr") public Department getDept() { return dept; } public void setDept(Department dept) { this.dept = dept; } }
注:
1.对于不维护关联关系, 没有外键的一方, 使用 @OneToOne 来进行映射, 建议设置 mappedBy=另一方外键。
2.因为此方不维护关联关系,所以直接使用@OneToOne的mappedBy属性,属性值对应的是另一方的外键属性名。
然后在配置文件persistence.xml中添加实体类的配置
<class>com.badao.jpa.helloworld.Manager</class> <class>com.badao.jpa.helloworld.Department</class>
添加位置如下
然后编写单元测试方法
@Test public void testOneToOnePersistence(){ Manager mgr = new Manager(); mgr.setMgrName("M-BB"); Department dept = new Department(); dept.setDeptName("D-BB"); //设置关联关系 mgr.setDept(dept); dept.setMgr(mgr); //执行保存操作 entityManager.persist(mgr); entityManager.persist(dept); }
注:
双向 1-1 的关联关系, 建议先保存不维护关联关系的一方, 即没有外键的一方, 这样不会多出 UPDATE 语句。
运行单元测试方法后查看数据库表
经理表
部门表