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

Source: Internet
Author: User

Unidirectional one-to-many association mappings

The mapped relational model:

Map file:

A one-to-many correlation mapping and a multiple-to-one correlation mapping principle are consistent, with the addition of a foreign key to one end of a multi-point

The difference between them is that they maintain different relationships:


* Many-to-one maintenance relationship is: a multi-point relationship, with this relationship, in the loading of many times can be loaded up


* One-to-many maintenance relationship is: a point to many relationships, with this relationship, when loading one can be more loaded up

Group.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="Group" table="T_group">
  7. <ID name="id">
  8. <generator class="native" />
  9. </ID>
  10. <property name="name" />
  11. <set name="users">
  12. <key column="GroupID"/>
  13. <one-to-many class="User"/>
  14. </Set>
  15. </class>
  16. </hibernate-mapping>

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" table="T_user">
  7. <ID name="id">
  8. <generator class="native" />
  9. </ID>
  10. <property name="name" />
  11. </class>
  12. </hibernate-mapping>

There is a flaw in maintaining relationships at one end:

* Because one end of the user does not know the existence of the group (that is, the user does not maintain the relationship with the group)
So when you save the user, the relationship field GroupID is null, and if the relationship field is set to non-empty, the
The data cannot be saved

* Also because the user does not maintain the relationship, and group maintains the relationship, the group will issue an extra UPDATE statement to ensure
Group is associated with user so that the group's user can be loaded when the group is loaded

To overcome these shortcomings, we generally change the one-way-to-multi-association mapping to bidirectional. As follows:

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

The purpose of using a one-to-many bi-directional association mapping is primarily to solve the defect of a multiple-way association, rather than demand-driven.

Map file:

A one-to-many bidirectional association mapping method:
* Use <key> tags on one end of the set, and add a foreign key at the end of the multiple
* Use <many-to-one> tag at one end of the multi
* <key> tags and <many-to-one> tags added to the field remain constant, otherwise it will result in data confusion

Group.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="Group" table="T_group">
  7. <ID name="id">
  8. <generator class="native" />
  9. </ID>
  10. <property name="name" />
  11. <set name="users" inverse="true">
  12. <key column="GroupID"/>
  13. <one-to-many class="User" />
  14. </Set>
  15. </class>
  16. </hibernate-mapping>

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" table="T_user">
  7. <ID name="id">
  8. <generator class="native" />
  9. </ID>
  10. <property name="name" />
  11. <many-to-one name="group" column="GroupID"/>
  12. </class>
  13. </hibernate-mapping>

Inverse properties:

* The inverse property can be used on a one-to-many and many-to-many bidirectional association, the inverse property defaults to False, False indicates that the local side can maintain the relationship, if the inverse is true, then the side cannot maintain the relationship, the other end will be left to maintain the relationship, the end of the failure.
One-to-many association mappings we usually maintain the relationship at a multiple end, so that a single end is invalidated, so setting inverse to True

*inverse is the reversal of the control direction, affecting only the storage

Analysis of Hibernate mapping (ii)--Relational mapping (4)

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.