Hibernate one-to-one correlation mapping configuration

Source: Internet
Author: User

First, one to the other association

Hibernate provides two ways to map one-to-one relationships: by foreign key mapping and by primary key mapping. The following is an example of the employee account and employee Profile table, which describes the two mapping methods and uses these two mappings to perform the following persistence actions:

(1) The employee file is saved and assigned to an employee account.

(2) Load the account information while loading the employee profile.

Follow foreign key mappings:

Diagram:

① creating entity classes: Resume, Users, and encapsulate attributes

public class Resume {    //file ID    private Integer resid;    File name    private String resname;    File number    private String rescardno;    Employee Object    private users users;
public class Users {   //employee ID    private Integer userid;    Employee name    private String username;    Employee Password    private String userpass;    File Object    private resume resume;}

② Configuring the Small configuration Users.hbm.xml

Note: property-ref= "Users" indicates that the users property of the resume establishes an association from the users object to the Resume object

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public     "-//hibernate/hibernate mapping DTD 3.0//en"    "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

Resume.hbm.xml

Note: Because the resume is a foreign key table implant <many-to-one> element and set the property unique= "True" ensures that the columns of the user profile can only be unique, one file corresponds to a user number

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"                            "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">

③ large configuration for associated small configurations

<!--associated small configuration--        <mapping resource= "Cn/happy/entity/users.hbm.xml"/> <mapping resource= "cn/happy/ Entity/resume.hbm.xml "/>

Test class:

          /* * Add *        /@Test public    void Addtest () {        //Create User objects users        u=new users ();        U.setusername ("Zhang 31");        U.setuserpass ("003");        Create archive object        resume R=new resume ();        R.setresname ("University diploma 1");        R.setrescardno ("003");        Association        U.setresume (r);        R.setusers (u);        Save the file, the employee automatically save        session.save (r);        System.out.println ("Save OK!!!");        }

Output Result:

Follow the primary key mappings:

The diagram is as follows:

Entity similar on

The second is a small configuration change. Resume primary key table, the UserID of users is both a primary key and a foreign key so the type of the implanted element generator is the foreign primary key

<one-to-one> implant attribute constrained to constrain the placement of foreign keys in the underlying data table

The Users.hbm.xml configuration file is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public     "-//hibernate/hibernate mapping DTD 3.0//en"    "Http://www.hibernate.org/dtd /hibernate-mapping-3.0.dtd ">

The Resume.hbm.xml configuration is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"                            "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">

The output results are as follows:

Hibernate one-to-one correlation mapping configuration

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.