现在演示hibernate的双向的多对多,举个例子吧Category和Item,一个Category有多个Item,一个Item可以对应多个属性,但这个时候我们需要三张表,多出来的一张表存放Category和Item的关联关系
Category.java
package cn.limbo.hibernate.manyTomany;
import java.util.HashSet;
import java.util.Set;
public class Category {
private Integer categoryId;
private String categoryName;
private Set<Item> items = new HashSet<>();
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Set<Item> getItems() {
return items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
@Override
public String toString() {
return "Category [categoryId=" + categoryId + ", categoryName=" + categoryName + ", items=" + items + "]";
}
}
Item.java
package cn.limbo.hibernate.manyTomany;
import java.util.HashSet;
import java.util.Set;
public class Category {
private Integer categoryId;
private String categoryName;
private Set<Item> items = new HashSet<>();
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Set<Item> getItems() {
return items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
@Override
public String toString() {
return "Category [categoryId=" + categoryId + ", categoryName=" + categoryName + ", items=" + items + "]";
}
}
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-7-22 23:51:50 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="cn.limbo.hibernate.manyTomany">
<class name="Category" table="CATEGORIES">
<id name="categoryId" type="java.lang.Integer">
<column name="CATEGORY_ID" />
<generator class="native" />
</id>
<property name="categoryName" type="java.lang.String">
<column name="CATEGORY_NAME" />
</property>
<!-- table:指定中间表 -->
<set name="items" table="CATEGORIES_ITEMS">
<key>
<column name="C_ID" />
</key>
<!--
使用many-to-many指定多对多的关联关系,column执行set集合中持久化类在中间表的外键列的名称
如Set中的持久化类是Item则对应的为I_ ID
-->
<many-to-many class="Item" column="I_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-7-22 23:51:50 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="cn.limbo.hibernate.manyTomany">
<class name="Item" table="ITEMS">
<id name="itemId" type="java.lang.Integer">
<column name="ITEM_ID" />
<generator class="native" />
</id>
<property name="itemName" type="java.lang.String">
<column name="ITEM_NAME" />
</property>
<!-- 需要注意的是,双向多对多的关联是,有一方必须指定inverse属性为true,使得这一方不维护关联关系,否则会出现组件重用异常 -->
<set name="categories" table="CATEGORIES_ITEMS" inverse="true">
<key>
<column name="I_ID"></column>
</key>
<many-to-many class="Category" column="C_ID"></many-to-many>
</set>
</class>
</hibernate-mapping>
Junit.java
package cn.limbo.hibernate.manyTomany;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
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;
public class Junit {
private SessionFactory sessionFactory = null;
private Session session = null;
private Transaction transaction = null;
@Before
public void init()
{
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy()
{
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testSave()
{
Category category1 = new Category();
category1.setCategoryName("C-AA");
Category category2 = new Category();
category2.setCategoryName("C-BB");
Item item1 = new Item();
item1.setItemName("I-AA");
Item item2 = new Item();
item2.setItemName("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.getItems());
}
}