hibernateノート(六)多対多


User.java
 
package com.goby.Hibernate.bean;

import java.util.Set;

public class User {
	private int id;
	private String name;
	private Set roles;
	public Set getRoles() {
		return roles;
	}
	public void setRoles(Set roles) {
		this.roles = 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;
	}
}

 
Role.java
package com.goby.Hibernate.bean;

import java.util.Set;

public class Role {
	private int id;
	private String name;
	private Set 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 getUsers() {
		return users;
	}
	public void setUsers(Set 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 
	package="com.goby.Hibernate.bean">

	<class name="User" table="t_user">
		
		<id name="id">
			<generator class="native"/>
		</id>
		
		<property name="name"></property><set name="roles" table="user_role" cascade="save-update" fetch="join">
			<key column="userid" />
			<many-to-many class="Role" column="roleid" />
		</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 
	package="com.goby.Hibernate.bean">

	<class name="Role" table="t_role">
		
		<id name="id">
			<generator class="native"/>
		</id>
		
		<property name="name"></property>
	<set name="users" table="user_role" cascade="save-update">
			<key column="roleid"/>
			<many-to-many class="User" column="userid"/>
		</set>

	</class>
	
</hibernate-mapping>

 
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
	<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_ManytoMany</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">seebig</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">false</property>
		
		<mapping resource="com/goby/Hibernate/bean/User.hbm.xml" />
		<mapping resource="com/goby/Hibernate/bean/Role.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 
 
test.java
package test;

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



import junit.framework.TestCase;

import org.hibernate.Session;

import com.goby.Hibernate.bean.HibernateUtils;
import com.goby.Hibernate.bean.Role;
import com.goby.Hibernate.bean.User;

public class test extends TestCase {
	
	public void testSave(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Role role1 = new Role();
			role1.setName("manager");
			
			Role role2 = new Role();
			role2.setName("officer");
			
			Role role3 = new Role();
			role3.setName("normal");
			
			Set roles1 = new HashSet();
			roles1.add(role1);
			roles1.add(role2);
			
			Set roles2 = new HashSet();
			roles2.add(role1);
			roles2.add(role3);
			
			User user1 = new User();
			user1.setName("goby");
			user1.setRoles(roles1);
			
			User user2 = new User();
			user2.setName("qq");
			user2.setRoles(roles2);
			
			session.save(user1);
			session.save(user2);
			
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
	
	/*
	 *  User      
	 */
	public void testload(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			User user = (User)session.load(User.class, 3);
			System.out.println("user.name="+user.getName());
			Set roles = user.getRoles();
			for(Iterator iter = roles.iterator();iter.hasNext();){
				Role role = (Role)iter.next();
				System.out.println("role.name="+role.getName());
			}
			
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
	
	/*
	 *  Role      
	 */
	public void testload2(){
		Session session = null;
		try{
			
			session = HibernateUtils.getSession();
			session.beginTransaction();
			
			Role role = (Role)session.load(Role.class, 1);
			System.out.println("role.name="+role.getName());
			Set users = role.getUsers();
			for(Iterator iter = users.iterator();iter.hasNext();){
				User user = (User)iter.next();
				System.out.println("user.name="+user.getName());
			}
			
			session.getTransaction().commit();
			
		}catch(Exception ex){
			ex.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			HibernateUtils.closeSession(session);
		}
	}
}