多对多关联映射

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

具体映射方式:
<set name="roles" table="t_user_role">
		<key column="userid"/>
		<many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>
</set>
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 void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

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



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;
	}
}
//Role.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Role" table="t_role">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
	</class>
</hibernate-mapping>


//User.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
		<set name="roles" table="t_user_role"><!-- Set标签映射Set集合 -->
			<key column="userid"/><!-- 生成第三方表,userid作为外键指向user -->
			<many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>
			      <!-- roleid作为外键指向Role -->
		</set>
	</class>
</hibernate-mapping>
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import junit.framework.TestCase;

public class Many2Many extends TestCase {

	public void testSave2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Role r1 = new Role();
			r1.setName("数据录入人员");
			session.save(r1);
			
			Role r2 = new Role();
			r2.setName("商务主管");
			session.save(r2);
			
			Role r3 = new Role();
			r3.setName("大区经理");
			session.save(r3);
			
			User u1 = new User();
			u1.setName("10");
			Set<Role> u1Roles = new HashSet<Role>();
			u1Roles.add(r1);
			u1Roles.add(r2);
			u1.setRoles(u1Roles);
			
			User u2 = new User();
			u2.setName("祖儿");
			Set<Role> u2Roles = new HashSet<Role>();
			u2Roles.add(r2);
			u2Roles.add(r3);
			u2.setRoles(u2Roles);
			
			User u3 = new User();
			u3.setName("杰伦");
			Set<Role> u3Roles = new HashSet<Role>();
			u3Roles.add(r1);
			u3Roles.add(r2);
			u3Roles.add(r3);
			u3.setRoles(u3Roles);
			
			session.save(u1);
			session.save(u2);
			session.save(u3);
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			User user = (User)session.load(User.class, 4); //根据主键加载用户
			System.out.println(user.getName());
			for (Iterator<Role> iter=user.getRoles().iterator(); iter.hasNext();) {
				Role role = iter.next();     // 根据用户获得其角色
				System.out.println(role.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
}


hibernate多对多关联映射(双向User<---->Role)

映射方法:
<set name="roles" table="t_user_role">
			<key column="userid"/>
			<many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>
</set>
table属性值必须和单向关联中的table属性值一致
<key>中column属性值要与单向关联中的<many-to-many>标签中的column属性值一致
在<many-to-many>中的column属性值要与单向关联中<key>标签的column属性值一致
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 void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

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



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 void setUsers(Set<User> users) {
		this.users = users;
	}

	public Set<User> getUsers() {
		return users;
	}
}
//Role.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.Role" table="t_role">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
		<set name="users" table="t_user_role" order-by="userid"><!-- 第三方表要一 致-->
			<key column="roleid"/><!-- roleid作为外键指向Role -->
			<many-to-many class="com.bjsxt.hibernate.User" column="userid"/>
			                 <!-- userid作为外键指向user -->
		</set>
	</class>
</hibernate-mapping>


//User.hbm.xml
<hibernate-mapping>
	<class name="com.bjsxt.hibernate.User" table="t_user">
		<id name="id">
			<generator class="native"/>
		</id>
		<property name="name" length = "10"/>
		<set name="roles" table="t_user_role"><!-- Set标签映射Set集合 -->
			<key column="userid"/><!-- 生成第三方表,userid作为外键指向user -->
			<many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/>
			      <!-- roleid作为外键指向Role -->
		</set>
	</class>
</hibernate-mapping>
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import junit.framework.TestCase;

public class Many2Many extends TestCase {

	public void testSave2() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Role r1 = new Role();
			r1.setName("数据录入人员");
			session.save(r1);
			
			Role r2 = new Role();
			r2.setName("商务主管");
			session.save(r2);
			
			Role r3 = new Role();
			r3.setName("大区经理");
			session.save(r3);
			
			User u1 = new User();
			u1.setName("10");
			Set<Role> u1Roles = new HashSet<Role>();
			u1Roles.add(r1);
			u1Roles.add(r2);
			u1.setRoles(u1Roles);
			
			User u2 = new User();
			u2.setName("祖儿");
			Set<Role> u2Roles = new HashSet<Role>();
			u2Roles.add(r2);
			u2Roles.add(r3);
			u2.setRoles(u2Roles);
			
			User u3 = new User();
			u3.setName("杰伦");
			Set<Role> u3Roles = new HashSet<Role>();
			u3Roles.add(r1);
			u3Roles.add(r2);
			u3Roles.add(r3);
			u3.setRoles(u3Roles);
			
			session.save(u1);
			session.save(u2);
			session.save(u3);
			
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
	public void testLoad1() {
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			session.beginTransaction();
			User user = (User)session.load(User.class, 4); //根据主键加载用户
			System.out.println(user.getName());
			for (Iterator<Role> iter=user.getRoles().iterator(); iter.hasNext();) {
				Role role = iter.next();     // 根据用户获得其角色
				System.out.println(role.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}		
	
	public void testLoad2() {
		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 = iter.next(); // 根据角色加载用户
				System.out.println(user.getName());
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally {
			HibernateUtils.closeSession(session);
		}
	}
}

你可能感兴趣的:(java,Hibernate,xml,JUnit)