(学习记录,错误不足之处,请您耐心指正^_^)
hibernate映射-双向n-n关联关系
一、代码示例:
{类文件↓}
Category.class
package com.zit.hibernate.n2n.both; import java.util.HashSet; import java.util.Set; public class Category { private Integer id; private String name; private Set<Item> items = new HashSet<>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Item> getItems() { return items; } public void setItems(Set<Item> items) { this.items = items; } }
Item.class
1 package com.zit.hibernate.n2n.both; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Item { 7 8 private Integer id; 9 private String name; 10 11 private Set<Category> categories = new HashSet<>(); 12 13 14 public Set<Category> getCategories() { 15 return categories; 16 } 17 public void setCategories(Set<Category> categories) { 18 this.categories = categories; 19 } 20 public Integer getId() { 21 return id; 22 } 23 public void setId(Integer id) { 24 this.id = id; 25 } 26 public String getName() { 27 return name; 28 } 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 }
{映射文件↓}
Category.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2015-12-25 13:55:35 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.zit.hibernate.n2n.both"> 6 <class name="Category" table="CATEGORIES"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="name" type="java.lang.String"> 12 <column name="NAME" /> 13 </property> 14 <set name="items" table="CATEGORIES_ITEMS"> 15 <key> 16 <column name="C_ID" /> 17 </key> 18 <many-to-many class="Item" column="I_ID"> 19 </many-to-many> 20 </set> 21 </class> 22 </hibernate-mapping>
Item.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2015-12-25 13:55:35 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.zit.hibernate.n2n.both"> 6 <class name="Item" table="ITEMS"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="name" type="java.lang.String"> 12 <column name="NAME" /> 13 </property> 14 15 <set name="categories" table="CATEGORIES_ITEMS" inverse="true"> 16 <key> 17 <column name="I_ID"></column> 18 </key> 19 <many-to-many class="Category" column="C_ID"> 20 </many-to-many> 21 </set> 22 23 </class> 24 </hibernate-mapping>
在Item与Category映射文件的配置中,两者应该交叉相同。如下:
1 Category.hbm.xml 2 <set name="items" table="CATEGORIES_ITEMS"> 3 <key> 4 <column name="C_ID" /> 5 </key> 6 <many-to-many class="Item" column="I_ID"> 7 </many-to-many> 8 </set> 9 10 11 Item.hbm.xml 12 <set name="categories" table="CATEGORIES_ITEMS" inverse="true"> 13 <key> 14 <column name="I_ID"></column> 15 </key> 16 <many-to-many class="Category" column="C_ID"> 17 </many-to-many> 18 </set>
(务必注意,双向多对多映射文件中,必须把其中一端的inverse设置为true,不让两端都维护关联关系,不然会造成主键冲突)
二、使用注意:
1.注意映射文件的配置。
(学习记录,错误不足之处,请您耐心指正^_^)