首页 > 代码库 > hibernate之 11.many2many双向

hibernate之 11.many2many双向

表结构:




类图:


CRUD:

User

package com.demo.model;

import java.util.Set;

/**用户信息
 * @author wobendiankun
 *2014-10-29 下午11:05:26
 */
public class User {
	/**
	 *用户Id 
	 */
	private int userId;
	/**
	 * 用户名
	 */
	private String userName;
	/**
	 * 密码
	 */
	private String password;
	
	private Student student ;
	private Set<Role> roles; 
	
	public User() {
	}
	
	
	public User(int userId) {
		this.userId = userId;
	}
	

	public User(String userName, String password) {
		this.userName = userName;
		this.password = password;
	}


	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}


	public Set<Role> getRoles() {
		return roles;
	}


	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
}

Role

package com.demo.model;

import java.util.Set;

/**角色信息
 * @author wobendiankun
 *2014-11-2 下午08:29:54
 */
public class Role {
	/**
	 * 角色id
	 */
	private int roleId;
	/**
	 * 角色名称
	 */
	private String roleName;
	private Set<User> users;
	
	public Role() {
	}
	
	public Role(int roleId) {
		this.roleId = roleId;
	}
	
	public Role(String roleName) {
		this.roleName = roleName;
	}

	
	public int getRoleId() {
		return roleId;
	}
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
}

配置:

User.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.demo.model.User" table="t_user">
		<id name="userId" column="user_id">
			<generator class="sequence">
				<param name="sequence">SEQ_T_USER</param>
			</generator>
		</id>
		<property name="userName" column="user_name" />
		<property name="password" />
		<many-to-one name="student" column="student_id" unique="true"></many-to-one>
		<set name="roles" table="t_user_role" inverse="true">
			<key column="user_id"></key>
			<many-to-many class="com.demo.model.Role" column="role_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>

Role.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.demo.model.Role" table="t_role">
		<id name="roleId" column="role_id">
			<generator class="sequence">
				<param name="sequence">SEQ_T_ROLE</param>
			</generator>
		</id>
		<property name="roleName" column="role_name" />
		<set name="users" table="t_user_role" lazy="extra">
			<key column="role_id"></key>
			<many-to-many class="com.demo.model.User" column="user_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>

add:

@Test
	public void addTest(){
		User user1=new User("赵三","111");
		User user2=new User("无四","111");
		Set<User> users=new HashSet<User>();
		users.add(user1);
		users.add(user2);
		
		Role role=new Role("财务部");
		Set<Role> roles=new HashSet<Role>();
		//设置双向关系
		role.setUsers(users);
		user1.setRoles(roles);
		user2.setRoles(roles);
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			session.save(role);
			session.save(user1);
			session.save(user2);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}

发出sql:

Hibernate: insert into t_role (role_name, role_id) values (?, ?)
Hibernate: insert into t_user (user_name, password, student_id, user_id) values (?, ?, ?, ?)
Hibernate: insert into t_user (user_name, password, student_id, user_id) values (?, ?, ?, ?)
Hibernate: insert into t_user_role (role_id, user_id) values (?, ?)
Hibernate: insert into t_user_role (role_id, user_id) values (?, ?)

load;

@Test
	public void loadTest(){
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			Role role=(Role)session.load(Role.class,3);
			System.out.println("角色名称:"+role.getRoleName());
			System.out.println("users:"+role.getUsers());
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			HibernateUtil.closeSession(session);
		}
	}


发出sql:

Hibernate: select role0_.role_id as role1_4_0_, role0_.role_name as role2_4_0_ from t_role role0_ where role0_.role_id=?
角色名称:财务部
Hibernate: select users0_.role_id as role2_4_1_, users0_.user_id as user1_1_, user1_.user_id as user1_2_0_, user1_.user_name as user2_2_0_, user1_.password as password2_0_, user1_.student_id as student4_2_0_ from t_user_role users0_ inner join t_user user1_ on users0_.user_id=user1_.user_id where users0_.role_id=?
users:[com.demo.model.User@1f31652, com.demo.model.User@3e96cf]



hibernate之 11.many2many双向