008——hibernate多对多关联映射

多对多关联映射分为单向多对多关联映射和双向多对多关联映射

单向多对多关联映射(单向User--------->Role):

具体映射:

<set name="roles" table="t_user_role">
<key column="user_id"/>
<many-to-many class="com.java.hibernate.Role" column="role_id"/>
</set>

实体类:

User.java

package com.java.hibernate;

import java.util.Set;

public class User {
	
	private int id;
	
	private String name;
	
	private Set<Role> roles;

	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<Role> getRoles() {
		return roles;
	}

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

Role.java

package com.java.hibernate;

public class Role {

	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;
	}
	
}

实体类配置文件:

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.java.hibernate.User" table="t_user">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name" />
		<set name="roles" table="t_user_role">
			<key column="user_id"></key>
			<many-to-many class="com.java.hibernate.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.java.hibernate.Role" table="t_role">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name" />
	</class>
</hibernate-mapping>

测试类:

Test.java

package com.java.hibernate;

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

import junit.framework.TestCase;

import org.hibernate.Session;

public class Test extends TestCase {

	public void testSave1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Role role1 = new Role();
			role1.setName("数据录入人员");
			session.save(role1);
			
			Role role2 = new Role();
			role2.setName("商务主管");
			session.save(role2);
			
			Role role3 = new Role();
			role3.setName("商务经理");
			session.save(role3);
			
			Role role4 = new Role();
			role4.setName("项目会计");
			session.save(role4);
			
			User user1 = new User();
			user1.setName("张三");
			Set<Role> uRole1 = new HashSet<Role>();
			uRole1.add(role1);
			uRole1.add(role2);
			user1.setRoles(uRole1);
			session.save(user1);
			
			User user2 = new User();
			user2.setName("李四");
			Set<Role> uRole2 = new HashSet<Role>();
			uRole2.add(role1);
			uRole2.add(role2);
			uRole2.add(role3);
			user2.setRoles(uRole2);
			session.save(user2);
			
			User user3 = new User();
			user3.setName("王五");
			Set<Role> uRole3 = new HashSet<Role>();
			uRole3.add(role3);
			uRole3.add(role4);
			user3.setRoles(uRole3);
			session.save(user3);
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
	}
	
	public void testLoad() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			User user = (User) session.load(User.class, 2);
			System.out.println(user.getName());
			for (Iterator iter = user.getRoles().iterator(); iter.hasNext();) {
				Role role = (Role) iter.next();
				System.out.println(role.getName());
			}
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}
	}
	
}<span style="color:#ff0000;">
</span>
双向多对多关联映射(双向User-------->Role)
映射方法:

<set name="users" table="t_user_role">
<key column="role_id" not-null="true"/>
<many-to-many class="com.java.hibernate.User" column="user_id"/>
</set>

注意:

  • 生成的中间表名称必须一样
  • 生成的中间表中的字段必须一样
实体类:
User.java
package com.java.hibernate;

import java.util.Set;

public class User {

	private int id;
	
	private String name;
	
	private Set<Role> roles;

	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<Role> getRoles() {
		return roles;
	}

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

import java.util.Set;

public class Role {
	
	private int id;
	
	private String name;
	
	private Set<User> users;

	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<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.java.hibernate.User" table="t_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name" />
		<set name="roles" table="t_user_role">
			<key column="user_id" not-null="true"></key>
			<many-to-many class="com.java.hibernate.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.java.hibernate.Role" table="t_role">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name" />
		<set name="users" table="t_user_role">
			<key column="role_id" not-null="true" />
			<many-to-many class="com.java.hibernate.User" column="user_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>
测试类
Test.java
package com.java.hibernate;

import java.util.Iterator;

import junit.framework.TestCase;

import org.hibernate.Session;

public class Test extends TestCase {

	public void testLoad() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			Role role = (Role)session.load(Role.class, 1);
			System.out.println(role.getName());
			for (Iterator<User> iter=role.getUsers().iterator(); iter.hasNext();) {
				User user = (User)iter.next();
				System.out.println(user.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值