In hibernate based on the primary key Mapping 1-1 Association relationship and the difference based on the foreign Key Mapping 1-1 Association relationship

Source: Internet
Author: User

Based on the primary key mapping 1-1 correlation relationship and based on the foreign key mapping 1-1 correlation relationship, the main difference is that the configuration map file will be different

Two persistence classes for manager and department

1: Based on primary key mapping 1-1 Association relationship

1) mapping files for entities that generate primary keys using the primary key of other persisted classes first need to specify a primary key generation mode of foreigner

The format is:

<id name= "DepartmentID" type= "Java.lang.Integer" >
<column name= "department_id"/>
<!--use a foreign key to generate the current primary key--
<generator class= "foreign" >
<param name= "Property" >manager</param>
</generator>
There is a PARAM element that specifies which property of the current persisted class uses the primary key as the foreign key

2) use the one-to-one element to map the associated property and must add Constrained= "true" to add foreign key constraints to the current primary key

The complete map file code is as follows
Department.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate Mapping DTD 3.0" "Http://hibernate.sourceforge.net/hibernate-mapping -3.0.dtd "> Package= "Com.cqupt.dayday" > <className= "Department" table= "Department" > <id name= "DepartmentID" type= "Java.lang.Integer" > <c Olumn name= "department_id"/> <!--using a foreign key to generate the current primary key--<generatorclass= "foreign" > <!--Property attribute specifies the primary key of which property of the current persisted class is used as a foreign key---<param Name= "Properties" >m anager</param> </generator> </id> <property name= "Departmentname" Type= "Java . lang. String "> <column name=" department_name "/> </property> <!--One end of the Foreigner primary key generator strategy is added-to-the one element maps the associated property, and oneThe-to-one property should also increase constrained= "true"to make the current primary key add a FOREIGN key constraint on the other end of the--<one-to-one name= "manager"class= "Manager" constrained= "true" ></one-to-one> </class>

Manager.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate Mapping DTD 3.0" "Http://hibernate.sourceforge.net/hibernate-mapping -3.0.dtd "> Package= "Com.cqupt.dayday" > <className= "Manager" table= "manager" > <id name= "ManagerID" type= "Java.lang.Integer" > <column nam E= "manager_id"/> <!--specify how the primary key is generated,native: Using Database Local mode-<generatorclass= "Native"/> </id> <property name= "ManagerName" type= "java.lang.String" > <column        Name= "Manager_name"/> </property> <!--mapping 1-1 relationship: There is already a foreign key in the corresponding data table, the current persistence class is mapped with one-to-one- <one-to-one name= "department"class= "Department" ></one-to-one> </class>

2: Based on the foreign key mapping 1-1 correlation relation Difference

1) Use Many-to-one in entity class mapping files using foreign keys to map 1-1 Association relationships, and you must specify Unique= "true"

That is, a department has only one manager, and one manager has only one department, and the corresponding managers in each department are different.
<many-to-one name= "Manager" class= "manager" column= "manager_id" unique= "true" ></many-to-one>

2) Entity classes that do not have foreign keys use one-to-one to map 1-1 Association relationships, and you must specify property-ref= "referenced by the associated entity class" To specify that a field other than the primary key of the associated entity be used as the associated field
<one-to-one name= "department" class= "department" property-ref= "manager" ></one-to-one>
The complete configuration file is as follows
Department.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate Mapping DTD 3.0" "Http://hibernate.sourceforge.net/hibernate-mapping -3.0.dtd "> Package= "Com.cqupt.dayday" > <className= "Department" table= "Department" > <id name= "DepartmentID" type= "Java.lang.Integer" > <c Olumn name= "department_id"/> <!--specify how the primary key is generated,native: Using Database Local mode-<generatorclass= "Native"/> </id> <property name= "Departmentname" type= "java.lang.String" > <col Umn name= "Department_name"/> </property> <!--use Many-to-one to map 1-1 relationships--<many-to -one name= "Manager"class= "Manager" column= "manager_id" unique= "true" ></many-to-one> </class>
Manager.hbm.xml
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate Mapping DTD 3.0" "Http://hibernate.sourceforge.net/hibernate-mapping -3.0.dtd "> Package= "Com.cqupt.dayday" > <className= "Manager" table= "manager" > <id name= "ManagerID" type= "Java.lang.Integer" > <column nam E= "manager_id"/> <!--specify how the primary key is generated,native: Using Database Local mode-<generatorclass= "Native"/> </id> <property name= "ManagerName" type= "java.lang.String" > <column        Name= "Manager_name"/> </property> <!--mapping 1-1 relationship: There is already a foreign key in the corresponding data table, the current persistence class is mapped with one-to-one- <!--There is no foreign key at one end to use the one-to-one element, which uses the Property-ref property to specify that fields other than the primary key of the associated entity are used as the associated field--<one-to-one name= "department "class= "Department" property-ref= "manager" ></one-to-one> </class>

In hibernate based on the primary key Mapping 1-1 Association relationship and the difference based on the foreign Key Mapping 1-1 Association relationship

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.