單向多對多關聯映射html
單向多對多關聯對象模型:node
映射後的關係模型:app
多對多映射,會產生第三張表來維護關係spa
映射文件:.net
User.hbm.xmlhibernate
- <?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.xmlxml
- <?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>
雙向多對多關聯映射htm
雙向多對多關聯對象模型對象
映射出的關係模型與單向同樣。blog
映射方法基本相同,只是在多的一端加入:
<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>
須要注意: * 生成的中間表名稱必須同樣 * 生成的中間表中的字段必須同樣