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

Source: Internet
Author: User

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?
  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. <set name= "roles" table="T_user_role">
  12. <key column="userid" />
  13. <many-to-many class="Role" column="Roleid" />
  14. </Set>
  15. </class>
  16. </hibernate-mapping>

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

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.