Bidirectional one-to-one correlation relationship
A department has a manager, a manager who cares about a department
Department entity class
PackageCom.jpa.helloworld2;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.Id;ImportJavax.persistence.JoinColumn;ImportJavax.persistence.OneToOne;Importjavax.persistence.Table;@Table(name="T_department")@Entity Public class Department { @Column(name="ID")@GeneratedValue @Id PrivateInteger ID;@Column(name="Dept_name")PrivateString Deptname;@JoinColumn(name="manager_id")@OneToOne PrivateManager Manager; PublicIntegergetId() {returnId } Public void setId(Integer ID) { This. id = ID; } PublicStringGetdeptname() {returnDeptname; } Public void Setdeptname(String deptname) { This. deptname = Deptname; } PublicManagerGetManager() {returnManager } Public void Setmanager(Manager manager) { This. Manager = Manager; }}
Manager entity Class
PackageCom.jpa.helloworld2;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.Id;ImportJavax.persistence.OneToOne;Importjavax.persistence.Table;@Table(name="T_manager")@Entity Public class Manager { @Column(name="ID")@GeneratedValue @Id PrivateInteger ID;@Column(name="NAME")PrivateString name;@OneToOne(mappedby="Manager")PrivateDepartment Department; PublicIntegergetId() {returnId } Public void setId(Integer ID) { This. id = ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicDepartmentgetdepartment() {returnDepartment } Public void setdepartment(Department Department) { This. Department = Department; }}
About the use of Mappedby, in the previous blog has been mentioned.
Insert
Manager manager = new Manager();manager.setName("mgr1");Department department = new Department();department.setDeptName("deptName1");manager.setDepartment(department);department.setManager(manager);entityManager.persist(manager);entityManager.persist(department);
For the order of insertions, insert a party that does not maintain an association, that is, the manager (a party that does not have a foreign key)
Query queries maintain a side of an association (a party with a foreign key)
Department dept = entityManager.find(Department.class1);System.out.println(dept.getDeptName());
The discovery executed two SQL statements, queried both the department and the manager
But we only want to department at this time, do not want manager. You can change to lazy load policy with fetch:
In the Department entity class
@OneToOne(fetch=FetchType.LAZY)private Manager manager;
Querying a party that does not maintain an association (a party without a foreign key)
Manager m = entityManager.find(Manager.class1);System.out.println(m.getName());
Run discovery, query Manager, will certainly go to check department, that is, using fetch to lazy loading strategy, the result is the same.
At different time, if lazy loading is changed, two SQL statement queries will be executed. By default, a SQL statement is executed to correlate the query. It is therefore not recommended to set fetch
There are certain reasons for querying two objects:
- When querying the manager table, there is no foreign key, and JPA does not know what to do with the associated object in the manager, so it will definitely go to the department table to see if there are any corresponding records.
- When querying department, because there are foreign keys, JPA is easy to know what to do with the associated object: either return a proxy or return an object.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JPA Learning Notes (9)--mapping bidirectional one-to-one correlation