【7.5.1】基于主键的双向多对多的关联映射

1.角色Role 和功能 Function

 

2.类Role 和Function类

public class Role {
	private int id;
	private String name;
	private Set<Function> functions = new HashSet<Function>(0);
	//get…set

}


public class Function {
	private int id;
	private String name;
	private String code;
	private String url;
	private Set<Role> roles = new HashSet<Role>(0);

	public Function() {
		// TODO Auto-generated constructor stub
	}
	public Function(String name, String code, String url) {
		super();
		this.name = name;
		this.code = code;
		this.url = url;
	}

	//get…set }

  

3.映射文件

Role.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.siggy.pojo">
	<class name="Function">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name"/>
		<property name="code"/>
		<property name="url"/>
		<set name="roles" table="role_func" inverse="true">
			<key column="fid"/>
			<many-to-many column="rid" class="Role"/>
		</set>
	</class>
</hibernate-mapping>

 

function.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.siggy.pojo">
	<class name="Role">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<!-- 多对多 -->
		<set name="functions" table="role_func" cascade="save-update">
			<!-- 表示当前类 映射到关系表中的列-->
			<key column="rid"/>
			<!-- 所对应的另一方在关系表中的列 -->
			<many-to-many column="fid" class="Function"/>
		</set>
	</class>
</hibernate-mapping>

  

4.测试代码

package cn.siggy.test;

import java.sql.SQLException;
import java.util.Iterator;

import javax.sql.rowset.serial.SerialException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import cn.siggy.pojo.Function;
import cn.siggy.pojo.Role;
import cn.siggy.util.HibernateUtil;

public class HibernateTest {
	@Test
	public void testCreateDB(){
		Configuration cfg = new Configuration().configure();
		SchemaExport se = new SchemaExport(cfg);
		//第一个参数 是否生成ddl脚本  第二个参数  是否执行到数据库中
		se.create(true, true);
	}
	@Test
	public void testSave() throws HibernateException, SerialException, SQLException{
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			Function f1 = new Function("用户管理","user_mag","userAction");
			Function f2 = new Function("角色管理","role_mag","roleAction");
			Function f3 = new Function("系统管理","sys_mag","sysAction");
			Function f4 = new Function("权限管理","prev_mag","prevAction");
			Role r1 = new Role();
			r1.setName("admin");
			r1.getFunctions().add(f1);
			r1.getFunctions().add(f2);
			r1.getFunctions().add(f3);
			r1.getFunctions().add(f4);
			Role r2 = new Role();
			r2.setName("vip");
			r2.getFunctions().add(f1);
			r2.getFunctions().add(f2);
			session.save(r1);
			session.save(r2);
			
			tx.commit();
			
		}catch (HibernateException e) {
			if(tx!=null)
				tx.rollback();
			e.printStackTrace();
			throw e;
		}finally{
			HibernateUtil.closeSession();
		}
	}
}

  

5.测试结果

1.执行testSave()

控制台信息:

Hibernate: 
    insert 
    into
        Role
        (name) 
    values
        (?)
Hibernate: 
    insert 
    into
        Function
        (name, code, url) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        Function
        (name, code, url) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        Function
        (name, code, url) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        Function
        (name, code, url) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        Role
        (name) 
    values
        (?)
Hibernate: 
    insert 
    into
        role_func
        (rid, fid) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        role_func
        (rid, fid) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        role_func
        (rid, fid) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        role_func
        (rid, fid) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        role_func
        (rid, fid) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        role_func
        (rid, fid) 
    values
        (?, ?)

  

数据库表:

 

 

posted @ 2017-04-07 22:46  chxbar  阅读(119)  评论(0编辑  收藏  举报