码迷,mamicode.com
首页 > Web开发 > 详细

hibernate 双向n-n

时间:2015-08-25 21:04:04      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

  • 领域模型:
    技术分享
  • 关系数据模型
    技术分享
  • 双向 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的 cloumn值为a,many-to-many 的 column 为b。则还有一边的 set 元素的 key 的 column 值 b,many-to-many的 column 值为 a. 

  • 对于双向 n-n 关联, 必须把当中一端的 inverse 设置为 true, 否则两端都维护关联关系可能会造成主键冲突.
    技术分享


代码具体解释:
Category.java
package com.atguigu.hibernate.n2n;

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.java
package com.atguigu.hibernate.n2n;

import java.util.HashSet;
import java.util.Set;

public class Item {

	private Integer id;
	private String name;
	
	private Set<Category> categories = 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<Category> getCategories() {
		return categories;
	}
	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}
	
}

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"> <hibernate-mapping package="com.atguigu.hibernate.n2n"> <class name="Category" table="CATEGORIES"> <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="CATEGORIES_ITEMS"> <key> <column name="C_ID" /> </key> <!-- 使用 many-to-many 指定多对多的关联关系. column 运行 Set 集合中的持久化类在中间表的外键列的名称 --> <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">

<hibernate-mapping>

    <class name="com.atguigu.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="CATEGORIES_ITEMS" inverse="true">
        	<key column="I_ID"></key>
        	<many-to-many class="com.atguigu.hibernate.n2n.Category" column="C_ID"></many-to-many>
        </set>
        
    </class>
</hibernate-mapping>


package com.atguigu.hibernate.n2n;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
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 destroy(){
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	@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()); 
	}
	
	@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);
	}

}


版权声明:本文博主原创文章。博客,未经同意,不得转载。

hibernate 双向n-n

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/4758279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!