A brief analysis of Hibernate mapping (II.)--Relational mapping (3)

Source: Internet
Author: User

Bidirectional one-to-one correlation mapping

As with one-way mapping, bidirectional mappings can still be implemented in two ways: 1, primary Key Association, 2, unique foreign Key Association

1. Primary Key Association

The mapped relational model is the same as one-way:

The difference between a mapping file and a one-way is that you need to add a <one-to-one> tag to the card to indicate that hibernate will load the associated object user on the primary key. <one-to-one> does not affect storage, only affects loading

User.hbm.xml

[HTML]View Plaincopyprint?
  1. <? XML version="1.0"?>
  2. <! DOCTYPE hibernate-mapping Public
  3. "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="Com.jialin.hibernate">
  6. <class name="User">
  7. <ID name="id">
  8. <!--use foreign policy to get the primary key of the associated object--
  9. <generator class="foreign">
  10. <!--property represents the object to associate--
  11. <param name="property">card</param>
  12. </Generator>
  13. </ID>
  14. <property name="name" />
  15. <one-to-one name="card" />
  16. </class>
  17. </hibernate-mapping>

Card.hbm.xml

[HTML]View Plaincopyprint?
  1. <? XML version="1.0"?>
  2. <! DOCTYPE hibernate-mapping Public
  3. "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="Com.jialin.hibernate">
  6. <class name="Card">
  7. <ID name="id">
  8. <generator class="native" />
  9. </ID>
  10. <property name="Cardno" />
  11. <!--<one-to-one> does not affect storage, only affects load-
  12. <one-to-one name="person" />
  13. </class>
  14. </hibernate-mapping>


2. Unique FOREIGN Key Association

The mapped relational model is the same as one-way:

Map file:

User.hbm.xml

[HTML]View Plaincopyprint?
  1. <? XML version="1.0"?>
  2. <! DOCTYPE hibernate-mapping Public
  3. "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="Com.jialin.hibernate">
  6. <class name="person">
  7. <ID name="id">
  8. <generator class="native" />
  9. </ID>
  10. <property name="name" />
  11. <many-to-one name="card" unique="true"></many-to-one>
  12. </class>
  13. </hibernate-mapping>

Card.hbm.xml

[HTML]View Plaincopyprint?
  1. <? XML version="1.0"?>
  2. <! DOCTYPE hibernate-mapping Public
  3. "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="Com.jialin.hibernate">
  6. <class name="Card">
  7. <ID name="id">
  8. <generator class="native" />
  9. </ID>
  10. <property name="Cardno" />
  11. <!--must specify <one-to-one>
  12. The Property-ref attribute in the label is the name of the relationship field-->
  13. <one-to-one name="person" property-ref="card"/>
[HTML]View Plaincopyprint?
      1. </class>
      2. Lt;/hibernate-mapping>

A brief analysis of Hibernate mapping (II.)--Relational mapping (3)

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.