Hibing relationship between Hibernate4.x-one-to-one ing and hibernate4.x one-to-one
Hibernate's 1-1 ing between 1-1 and 1 is mainly divided into two types:
1. 1-to-1 Relationship of Hibernate based on foreign key ing
For a 1-1 association based on foreign keys, the foreign keys can be stored on any side, and the external-to-one element is added at the end of the foreign key to be stored. Add the unique = "true" attribute to the sequence-to-one element to indicate a 1-1 Association.
1 <many-to-one name="mgr" class="com.yl.hibernate.one2one.foreign.Manager" column="MGR_ID" unique="true"></many-to-one>
The other end uses the one-to-one element, which uses the property-ref attribute to specify fields other than the primary key of the correlated object as the correlated field.
1 <one-to-one name="dept" class="com.yl.hibernate.one2one.foreign.Department" property-ref="mgr"></one-to-one>
-- If the property-ref attribute is not used, the SQL statement is as follows:
1 from2 MANAGERS manager0_ 3 left outer join4 DEPARTMENTS department1_ 5 on manager0_.MGR_ID=department1_.DEPT_ID
-- If the property-ref attribute is used, the SQL statement is as follows:
1 from2 MANAGERS manager0_ 3 left outer join4 DEPARTMENTS department1_ 5 on manager0_.MGR_ID=department1_.MGR_ID
------------------------------------ Code ------------------------------------------------------------------
Department. hbm. xml
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5
Manager. hbm. xml
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5
2. 1-to-1 Relationship of Hibernate based on primary key ing
Primary Key-based ing policy: a primary key generator at one end uses the foreign policy to generate its own primary key based on the primary key of the "peer". The primary key cannot be generated independently. <Param> the child element specifies which attribute of the current persistence class is used as the "peer"
1 <id name = "deptId" type = "java. lang. Integer"> 2 <column name = "DEPT_ID"/> 3 <! -- Use a foreign key to generate the current primary key --> 4 <generator class = "foreign"> 5 <! -- Property Attribute specifies the primary key of the current persistence class as the foreign key --> 6 <param name = "property"> mgr </param> 7 </generator> 8 </id>
The one-to-one element ing Association attribute is added at one end of the foreign primary key generator policy. The one-to-one attribute should also contain the constrained = "true" attribute; add one-to-one element ing Association attribute on the other end
Constrained: Specifies to add a foreign key constraint to the primary key of the database table corresponding to the current persistence class, and reference the primary key of the database table corresponding to the associated object ("peer ").
------------------------------------ Code ------------------------------------------------------------------
Department. hbm. xml
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5
Manager. hbm. xml
1 <? Xml version = "1.0"?> 2 <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <! -- Generated 2014-11-29 20:05:53 by Hibernate Tools 3.4.0.CR1 --> 5