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?
- <? XML version="1.0"?>
- <! DOCTYPE hibernate-mapping Public
- "-//hibernate/hibernate Mapping DTD 3.0//en"
- "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="Com.jialin.hibernate">
- <class name="Group" table="T_group">
- <ID name="id">
- <generator class="native" />
- </ID>
- <property name="name" />
- <set name="users">
- <key column="GroupID"/>
- <one-to-many class="User"/>
- </Set>
- </class>
- </hibernate-mapping>
User.hbm.xml
[HTML]View Plaincopyprint?
- <? XML version="1.0"?>
- <! DOCTYPE hibernate-mapping Public
- "-//hibernate/hibernate Mapping DTD 3.0//en"
- "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="Com.jialin.hibernate">
- <class name="User" table="T_user">
- <ID name="id">
- <generator class="native" />
- </ID>
- <property name="name" />
- </class>
- </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?
- <? XML version="1.0"?>
- <! DOCTYPE hibernate-mapping Public
- "-//hibernate/hibernate Mapping DTD 3.0//en"
- "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="Com.jialin.hibernate">
- <class name="Group" table="T_group">
- <ID name="id">
- <generator class="native" />
- </ID>
- <property name="name" />
- <set name="users" inverse="true">
- <key column="GroupID"/>
- <one-to-many class="User" />
- </Set>
- </class>
- </hibernate-mapping>
User.hbm.xml
[HTML]View Plaincopyprint?
- <? XML version="1.0"?>
- <! DOCTYPE hibernate-mapping Public
- "-//hibernate/hibernate Mapping DTD 3.0//en"
- "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="Com.jialin.hibernate">
- <class name="User" table="T_user">
- <ID name="id">
- <generator class="native" />
- </ID>
- <property name="name" />
- <many-to-one name="group" column="GroupID"/>
- </class>
- </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)