. Hibernate one-to-one mapping and component mapping

Source: Internet
Author: User

1. Follow the foreign key mapping ( Hibernate provides two ways to map one-to-one relationships: by foreign key mapping and by primary key mapping


Implementation needs:

Create an entity class Users1 and Resume1

Public class Users1 {

Private Integer userid;

Private String username;

Private String Userpass;

Private Resume1 resume1;

}

***********

Public class Resume1 {

Private Integer Resid;

Private String Resname;

Private String Rescardno;

Private Users1 users1;

}

-----------------------

configuration file Users1.hbm.xml and Resume1.hbm.xml:

Users1.hbm.xml

<?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" >

"CN.HAPPY.ONETOONE_FK">

<class name="Users1" table="USERS1">

<id name="userid " column="userid " >

<generator class="native"></generator>

</id>

<property name="username" column="username" type="string"></property>

<property name="Userpass" column="Userpass" type="string"></property>

<one-to-one name="resume1" class="Resume1" property-ref="users1" ></one-to-one >

</class>

*********************

Resume1.hbm.xml

<?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" >

"CN.HAPPY.ONETOONE_FK">

<class name="Resume1" table="RESUME1">

<id name="Resid" column="Resid" >

<generator class="native"></generator>

</id>

<property name="Resname" column="Resname" type="string"></property>

<property name="Rescardno" column="Rescardno" type="string"></property >

<many-to-one name="users1" class="Users1" cascade="All" column="Resuserid " Unique="true"></many-to-one>

</class>

------------------------------------

2. Primary KEY Mapping

Implementation needs:

Create entity Classes Users2 and Resume2

Public class Users2 {

Private Integer userid;

Private String username;

Private String Userpass;

Private Resume2 resume2;

}

******************* :

Public class Resume2 {

Private Integer Resid;

Private String Resname;

Private String Rescardno;

Private Users2 users2;

}

----------------------------

configuration file Users1.hbm.xml and Resume1.hbm.xml

Users1.hbm.xml

<?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" >

"CN.HAPPY.ONETOONE_PK">

<class name="Users2" table="USERS2">

<id name="userid " column="userid " >

<generator class="foreign">

<param name="property">resume2</param>

</generator>

</id>

<property name="username" column="username" type="string"></property>

<property name="Userpass" column="Userpass" type="string"></property>

<one-to-one name="resume2" class="Resume2" constrained="true"></one-to-one >

</class>

************************

Resume2.hbm.xml

<?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" >

"CN.HAPPY.ONETOONE_PK">

<class name="Resume2" table="RESUME2">

<id column="RESID" name="RESID">

<generator class="native"/>

</id>

<property column="Resname" name="Resname" type="string"/>

<property column="Rescardno" name="Rescardno" type="string"/>

<one-to-one name="users2" cascade="All" class="Users2"/>

</class>

==========================================

One. component Mapping (an important principle of establishing a relational data model is to minimize the number of database tables and the foreign key references between tables without causing data redundancy)

Implementation requirements:

Creating Emphomeaddress and empinfo

Public class emphomeaddress {

Private String Ehomestreet;

Private String ehomecity;

Private String ehomeprovince;

Private String Ehomezipcode;

Private Empinfo Empinfo;

}

--------------

Public class Empinfo {

Private Integer Eid;

Private String ename;

Private Emphomeaddress ehome;

}

*************************

Creating a configuration file EmpInfo.hbm.xml

<?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" >

"Cn.happy.component">

<class name="Empinfo" table="Empinfo">

<id name="Eid " column="Eid" >

<generator class="native"></generator>

</id>

<property name="ename" column="ename" type="string"></property>

<component name="ehome" class="emphomeaddress">

<parent name="Empinfo"/>

<property name="Ehomestreet" column="Ehomestreet" type="string"></property >

<property name="ehomecity" column="ehomecity" type="string"></property>

<property name="ehomeprovince" column="ehomeprovince" type="string"></ Property>

<property name="Ehomezipcode" column="Ehomezipcode" type="string"></ Property>

</component>

</class>

. Hibernate one-to-one mapping and component mapping

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.