mapping One-to-one associations
Hibernate provides two ways to map one-to-one relationships, in terms of foreign key mappings and by primary key mappings.
Here is the employee table and the Profile information table (the relationship between the employee and the file table is a one-to-two relationship)
File Table (Dept)
1 Public classDept {2 PrivateInteger DeptID;3 PrivateString Mystreet;4 PrivateEMP EMP;5 6 PublicDept () {7 }8 9 PublicDept (Integer deptid, String mystreet, emp emp) {Ten This. DeptID =DeptID; One This. Mystreet =Mystreet; A This. EMP =EMP; - } - PublicInteger Getdeptid () { the returnDeptID; - } - Public voidSetdeptid (Integer deptid) { - This. DeptID =DeptID; + } - PublicString Getmystreet () { + returnMystreet; A } at Public voidSetmystreet (String mystreet) { - This. Mystreet =Mystreet; - } - PublicEmp getemp () { - returnEMP; - } in Public voidsetemp (emp emp) { - This. EMP =EMP; to } + -}
Employee table (EMP)
1 Public classEMP {2 PrivateInteger Empid;3 PrivateString myname;4 PrivateDept Dept;5 6 PublicEmp () {7 }8 9 PublicEmp (Integer empid, String myname, Dept Dept) {Ten This. Empid =Empid; One This. myname =myname; A This. Dept =Dept; - } - PublicInteger getempid () { the returnEmpid; - } - Public voidsetempid (Integer empid) { - This. Empid =Empid; + } - PublicString Getmyname () { + returnmyname; A } at Public voidsetmyname (String myname) { - This. myname =myname; - } - PublicDept getdept () { - returnDept; - } in Public voidsetdept (Dept Dept) { - This. Dept =Dept; to } +}
Here's the first scenario where you follow the primary key mapping
EMP's configuration file
1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE hibernate-Mapping Public3"-//hibernate/hibernate Mapping DTD 3.0//en"4"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >5 Package= "CN.RM.ONETOONE_PK" >6<className= "EMP" table= "EMP2016" >7<id name= "Empid" column= "Empid" type= "int" >8<generatorclass= "Sequence" >9<param name= "sequence" >sq_id</param>Ten</generator> One</id> A<property name= "myname" column= "MyName" type= "string" ></property> -<one-to-one name= "Dept"class= "Dept" property-ref= "EMP" ></one-to-one> -<!--property-ref for EMP indicates that the EMP property of dept has been set up from the EMP object to the Dept object. the</class> - -View CodeConfiguration file for Dept
1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE hibernate-Mapping Public3"-//hibernate/hibernate Mapping DTD 3.0//en"4"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >5 Package= "CN.RM.ONETOONE_PK" >6<className= "Dept" table= "DEPT2016" >7<id name= "DeptID" column= "DeptID" type= "int" >8<generatorclass= "Sequence" >9<param name= "sequence" >hb_ids</param>Ten</generator> One</id> A<property name= "Mystreet" column= "Mystreet" type= "string" ></property> -<many-to-one name= "EMP"class= "EMP" cascade= "All" unique= "true" column= "Resuserid" ></many-to-one> -<!--the Cascade property of all indicates that the table associated with additions or deletions to this table will automatically update unique, and the property is true to indicate that the unique constraint column= "Resuserid" dept the public column of the table-- the</class> -View CodeThis scenario is characterized by the addition of a column column= "Resuserid" in the Dept table, which is the column associated with the EMP.
The second option is to follow the foreign key mapping
EMP's configuration file
1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE hibernate-Mapping Public3"-//hibernate/hibernate Mapping DTD 3.0//en"4"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >5 Package= "CN.RM.ONETOONE_FK" >6<className= "EMP" table= "EMP2016FK" >7<id name= "Empid" column= "Empid" type= "int" >8<generatorclass= "foreign" >9<param name= "Property" >dept</param>Ten</generator> One</id> A<property name= "myname" column= "MyName" type= "string" ></property> -<one-to-one name= "Dept"class= "Dept" constrained= "true" ></one-to-one> -<!--constrained= "True" indicates that the primary key in the EMP table is referenced as a foreign key in the EMP table. the</class> -View CodeConfiguration file for Dept
1<?xml version= "1.0" encoding= "UTF-8"?>2<! DOCTYPE hibernate-Mapping Public3"-//hibernate/hibernate Mapping DTD 3.0//en"4"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >5 Package= "CN.RM.ONETOONE_FK" >6<className= "Dept" table= "DEPT2016FK" >7<id name= "DeptID" column= "DeptID" type= "int" >8<generatorclass= "Sequence" >9<param name= "sequence" >hb_ids</param>Ten</generator> One</id> A<property name= "Mystreet" column= "Mystreet" type= "string" ></property> -<one-to-one name= "EMP"class= "EMP" cascade= "All" ></one-to-one> -</class> theView CodeThe feature of this scheme is that the primary key of the employee table and the primary key in the Dept table are the same, that is, the EMP table and the Dept table share the same OID
Hibernate maps one-to-one affinity relationships