n-n的关联关系分为以下两类:
1.单向n-n关联关系:
- n-n的关联必须使用连接表
- 与1-n映射类似,必须为set集合元素添加key子元素,指定CATEGORIES_ITEMS表中参照CATEGORIES表的外键为CATEGORIY_ID。与1-n关联映射不同的是,建立n-n关联时,集合中的元素使用many-to-many。many-to-many子元素的class属性指定items接种存放的是Item对象,column属性指定CATEGORIES_ITEMS表中参照ITEMS表的外键为ITEM_ID
--------------------------------代码--------------------------------------------------------
Category.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="com.yl.hibernate.n2n">
<class name="Category" table="CATEGORYS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<!-- table:指定中间表 -->
<set name="items" table="CATEGORIRIES_ITEMS" inverse="false" lazy="true">
<key>
<column name="C_ID" />
</key>
<!-- 使用many-to-many 指定多对多的关联关系, column指定set集合中的持久化类在中间表的外键列的名称 -->
<many-to-many class="Item" column="I_ID"/>
</set>
</class>
</hibernate-mapping>
Item.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.yl.hibernate.n2n.Item" table="ITEMs">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>
2.双向n-n关联关系
- 双向n-n关联需要两端都使用集合属性
- 双向n-n关联必须使用连接表
- 集合属性应增加key子元素用以映射外键列,集合元素里还应增加many-to-many子元素关联实体类
- 在双向n-n关联的两边都需指定连接表的表名及外键列的列名。两个集合元素set的table元素的值必须指定,且必须相同。set元素的两个子元素:key和many-to-many多必须指定column属性,其中,key和many-to-many分别制定本持久化类和关联类在连接表中的外键列名,因此两边的key与many-to-many的column属性交叉相同。也就是说,一边的set元素的key的column值为a,many-to-many的column为b,则另一边的set元素的key的column值为b,many-to-many的column值为a
- 对于双向n-n关联,必须把其中一端的inverse设置为true,否则两端都维护关联关系可能会造成逐渐冲突
Category.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="com.yl.hibernate.n2n">
<class name="Category" table="CATEGORYS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<!-- table:指定中间表 -->
<set name="items" table="CATEGORIRIES_ITEMS" inverse="false" lazy="true">
<key>
<column name="C_ID" />
</key>
<!-- 使用many-to-many 指定多对多的关联关系, column指定set集合中的持久化类在中间表的外键列的名称 -->
<many-to-many class="Item" column="I_ID"/>
</set>
</class>
</hibernate-mapping>
Item.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.yl.hibernate.n2n.Item" table="ITEMs">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property> <set name="categories" table="CATEGORIRIES_ITEMS" inverse="true">
<key><column name="I_ID"></column></key>
<many-to-many class="com.yl.hibernate.n2n.Category" column="C_ID"></many-to-many>
</set> </class>
</hibernate-mapping>
测试类:
package com.yl.hibernate.n2n; import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class HibernateTest { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void init() {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction();
}
@After
public void destory() {
transaction.commit(); session.close(); sessionFactory.close();
} @Test
public void testSave() {
Category category1 = new Category();
category1.setName("C-AA"); Category category2 = new Category();
category2.setName("C-BB"); Item item1 = new Item();
item1.setName("I-AA"); Item item2 = new Item();
item2.setName("I-BB"); //设定关联关系
category1.getItems().add(item1);
category1.getItems().add(item2); category2.getItems().add(item1);
category2.getItems().add(item2); item1.getCategories().add(category1);
item1.getCategories().add(category2); item2.getCategories().add(category1);
item2.getCategories().add(category2); //执行保存操作
session.save(category1);
session.save(category2); session.save(item1);
session.save(item2);
} @Test
public void testGet() {
Category category = (Category) session.get(Category.class, 1);
System.out.println(category.getName());
//需要连接中间表
Set<Item> items = category.getItems();
System.out.println(items.size()); } }