业务场景:
剃须刀属于家电,也属于日常用品。这种情况就是多对多关系。
一般会多一张表来表示两张表的关系,三张表如下
category:类别表
item:项目表
category_times:关联表,维护category、item的关系
单向多对多-关键代码:
<!-- table:指定中间表 --> <set name="items" table="CATEGORY_TIMES"> <key> <column name="CATEGORY_ID"></column> </key> <!-- 使用many-to-many指定多对多的关联关系 column:执行Set集合的持久化类在中间表的外键列的名称 --> <many-to-many class="com.shuoeasy.test.Item" column="ITEM_ID"></many-to-many> </set>
单向多对多-详细代码:
Category.java:
package com.shuoeasy.test; import java.util.HashSet; import java.util.Set; public class Category { private int id; private String name; private Set<Item> items = new HashSet<>(); public int getId() { return id; } public void setId(int 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; } @Override public String toString() { return "Category [id=" + id + ", name=" + name + "]"; } }
Item.java:
package com.shuoeasy.test; public class Item { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Item [id=" + id + ", name=" + name + "]"; } }
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 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.shuoeasy.test.Category" table="CATEGORY"> <id name="id" type="int"> <column name="ID" /> <!-- 指定主键的生成方式 --> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <!-- table:指定中间表 --> <set name="items" table="CATEGORY_TIMES"> <key> <column name="CATEGORY_ID"></column> </key> <!-- 使用many-to-many指定多对多的关联关系 column:执行Set集合的持久化类在中间表的外键列的名称 --> <many-to-many class="com.shuoeasy.test.Item" column="ITEM_ID"></many-to-many> </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 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.shuoeasy.test.Item" table="ITEM"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> </class> </hibernate-mapping>
AppTest.java:
package com.shuoeasy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { Session session; SessionFactory sf; @Before public void init() { Configuration conf = new Configuration().configure(); ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); sf = conf.buildSessionFactory((org.hibernate.service.ServiceRegistry) sr); session = sf.openSession(); session.beginTransaction(); System.out.println("init"); } @After public void destory() { session.getTransaction().commit(); session.close(); sf.close(); System.out.println("dectory"); } /** * 插入数据 */ @Test public void testInsert(){ // 创建对象 Category category1 = new Category(); category1.setName("家电"); Category category2 = new Category(); category2.setName("日常用品"); Item item1 = new Item(); item1.setName("剃须刀"); Item item2 = new Item(); item2.setName("电饭锅"); // 设定关联关系 category1.getItems().add(item1); category1.getItems().add(item2); category2.getItems().add(item2); // 执行保存操作 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); for(Item item : category.getItems()){ System.out.println(item); } } }
生成sql:
Hibernate: select category0_.ID as ID1_0_0_, category0_.NAME as NAME2_0_0_ from CATEGORY category0_ where category0_.ID=? Category [id=1, name=家电] Hibernate: select items0_.CATEGORY_ID as CATEGORY1_0_0_, items0_.ITEM_ID as ITEM_ID2_1_0_, item1_.ID as ID1_2_1_, item1_.NAME as NAME2_2_1_ from CATEGORY_TIMES items0_ inner join ITEM item1_ on items0_.ITEM_ID=item1_.ID where items0_.CATEGORY_ID=?
表结构
category:
item:
category_times:
单向多对多-关键代码
1.加上属性。 get、set方法略 private Set<Category> categorys = new HashSet<>(); 2.配置文件 <set name="categorys" sort="unsorted" table="CATEGORY_TIMES" inverse="true"> <key column="ITEM_ID" /> <many-to-many class="com.shuoeasy.test.Category" column="CATEGORY_ID" unique="false" /> </set>
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 2016-6-25 14:50:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping default-access="field"> <class name="com.shuoeasy.test.Item" table="ITEM"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <property generated="never" lazy="false" name="name" type="java.lang.String"> <column name="NAME" /> </property> <set name="categorys" sort="unsorted" table="CATEGORY_TIMES" inverse="true"> <key column="ITEM_ID" /> <many-to-many class="com.shuoeasy.test.Category" column="CATEGORY_ID" unique="false" /> </set> </class> </hibernate-mapping>