One-way many-to-many association mappings
One-way many-to-many association object model:
The mapped relational model:
Many-to-many mappings produce a third table to maintain relationships
Map file:
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" />
- <set name= "roles" table="T_user_role">
- <key column="userid" />
- <many-to-many class="Role" column="Roleid" />
- </Set>
- </class>
- </hibernate-mapping>
Role.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="Role" table="T_role">
- <ID name="id">
- <generator class="native" />
- </ID>
- <property name="name" />
- </class>
- </hibernate-mapping>
Bidirectional many-to-many association mappings
Bidirectional many-to-many association object model
The mapped relational model is the same as the one-way.
The mapping method is basically the same, just adding at the end of the multiple:
<set name= "Users" table= "T_user_role" >
<key column= "role_id" not-null= "true"/>
<many-to-many class= "Com.bjpowernode.hibernate.User" column= "user_id"/>
</set>
Need to note:
* The resulting intermediate table name must be the same
* The fields in the resulting intermediate table must be the same
A brief analysis of Hibernate mapping (II.)--Relational mapping (5)