Hibernate4.x之映射关系--多对多
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
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 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.yl.hibernate.n2n"> 6 <class name="Category" table="CATEGORYS"> 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 <!-- table:指定中间表 --> 15 <set name="items" table="CATEGORIRIES_ITEMS" inverse="false" lazy="true"> 16 <key> 17 <column name="C_ID" /> 18 </key> 19 <!-- 使用many-to-many 指定多对多的关联关系, column指定set集合中的持久化类在中间表的外键列的名称 --> 20 <many-to-many class="Item" column="I_ID"/> 21 </set> 22 </class> 23 </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 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.yl.hibernate.n2n.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 </class> 15 </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
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 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping package="com.yl.hibernate.n2n"> 6 <class name="Category" table="CATEGORYS"> 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 <!-- table:指定中间表 --> 15 <set name="items" table="CATEGORIRIES_ITEMS" inverse="false" lazy="true"> 16 <key> 17 <column name="C_ID" /> 18 </key> 19 <!-- 使用many-to-many 指定多对多的关联关系, column指定set集合中的持久化类在中间表的外键列的名称 --> 20 <many-to-many class="Item" column="I_ID"/> 21 </set> 22 </class> 23 </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 2014-11-29 21:45:04 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="com.yl.hibernate.n2n.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="CATEGORIRIES_ITEMS" inverse="true"> 16 <key><column name="I_ID"></column></key> 17 <many-to-many class="com.yl.hibernate.n2n.Category" column="C_ID"></many-to-many> 18 </set> 19 20 </class> 21 </hibernate-mapping>
测试类:
1 package com.yl.hibernate.n2n; 2 3 4 import java.util.Set; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration; 10 import org.hibernate.service.ServiceRegistry; 11 import org.hibernate.service.ServiceRegistryBuilder; 12 import org.junit.After; 13 import org.junit.Before; 14 import org.junit.Test; 15 16 public class HibernateTest { 17 18 private SessionFactory sessionFactory; 19 private Session session; 20 private Transaction transaction; 21 22 @Before 23 public void init() { 24 Configuration configuration = new Configuration().configure(); 25 ServiceRegistry serviceRegistry = 26 new ServiceRegistryBuilder().applySettings(configuration.getProperties()) 27 .buildServiceRegistry(); 28 29 sessionFactory = configuration.buildSessionFactory(serviceRegistry); 30 31 session = sessionFactory.openSession(); 32 33 transaction = session.beginTransaction(); 34 } 35 @After 36 public void destory() { 37 transaction.commit(); 38 39 session.close(); 40 41 sessionFactory.close(); 42 } 43 44 @Test 45 public void testSave() { 46 Category category1 = new Category(); 47 category1.setName("C-AA"); 48 49 Category category2 = new Category(); 50 category2.setName("C-BB"); 51 52 53 Item item1 = new Item(); 54 item1.setName("I-AA"); 55 56 Item item2 = new Item(); 57 item2.setName("I-BB"); 58 59 //设定关联关系 60 category1.getItems().add(item1); 61 category1.getItems().add(item2); 62 63 category2.getItems().add(item1); 64 category2.getItems().add(item2); 65 66 item1.getCategories().add(category1); 67 item1.getCategories().add(category2); 68 69 item2.getCategories().add(category1); 70 item2.getCategories().add(category2); 71 72 //执行保存操作 73 session.save(category1); 74 session.save(category2); 75 76 session.save(item1); 77 session.save(item2); 78 } 79 80 81 @Test 82 public void testGet() { 83 Category category = (Category) session.get(Category.class, 1); 84 System.out.println(category.getName()); 85 //需要连接中间表 86 Set<Item> items = category.getItems(); 87 System.out.println(items.size()); 88 89 } 90 91 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。