Hibernate-----One-to-one shared primary key association mappings (map File mode)

Source: Internet
Author: User

Shared primary key affinity mapping: Let two objects have the same primary key value to indicate a one-to-one correspondence between them; database tables do not have additional fields to maintain their relationships, only through the table's primary key

One, one-way Association mappings:

One (Maintenance side: Person)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping dtd3.0//en" "Http://hibernate.sourceforge.net/hiber Nate-mapping-3.0.dtd ">

To

One (non-maintenance side: Idcard)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

public void Testonetoone () {       session session = NULL;       try {           session = Hibernateutils.getsession ();           Session.begintransaction ();           Idcard idcard = new Idcard ();           Idcard.setcardno ("111111");             Person person           = new person ();           Person.setname ("name");           Person.setidcard (Idcard);           The attribute of a one-to-one correlation mapping determines that the associated object (Idcard) is saved when the person is saved (by default cascading properties), so the save person can be saved idcard           //maintenance end to save           Session.save ( person);           Session.gettransaction (). commit ();       } catch (Exception e) {           e.printstacktrace ();           Session.gettransaction (). rollback ();       } finally {           hibernateutils.closesession (session);       }    }




Two, two-way association mapping

One (person: maintenance side)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

To

One (Idcard: non-maintenance)

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping public    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">






Hibernate-----One-to-one shared primary key association mappings (map File mode)

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.