package com.jonsion.hibernate;
import java.util.Set;
public class Role {
private int id;
private String name;
private Set users;
public int getId() {
return id;
}
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.jonsion.hibernate;
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;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
<hibernate-mapping >
<class name="com.jonsion.hibernate.Role">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="users" table="t_user_role">
<key column="roleid"/>
<many-to-many class="com.jonsion.hibernate.User" column="userid"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
<hibernate-mapping >
<class name="com.jonsion.hibernate.User">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="roles" table="t_user_role">
<key column="userid"/>
<many-to-many class="com.jonsion.hibernate.Role" column="roleid"/>
</set>
</class>
</hibernate-mapping>
package com.jonsion.hibernate;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Many2ManyTest extends TestCase{
public void testSave1(){
Session session=null;
Transaction tx=null;
try{
session=HibernateUtils.getSession();
tx=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 user1=new User();
user1.setName("阿希");
Set r1role=new HashSet();
r1role.add(r1);
r1role.add(r2);
user1.setRoles(r1role);
User user2=new User();
user2.setName("阿雅");
Set r2role=new HashSet();
r2role.add(r2);
r2role.add(r3);
user2.setRoles(r2role);
User user3=new User();
user3.setName("阿杜");
Set r3role=new HashSet();
r3role.add(r1);
r3role.add(r2);
r3role.add(r3);
user3.setRoles(r3role);
session.save(user1);
session.save(user2);
session.save(user3);
tx.commit();
}catch(Exception ex){
ex.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
public void testSave2(){
Session session=null;
Transaction tx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
User user1=new User();
user1.setName("阿希");
session.save(user1);
User user2=new User();
user2.setName("阿雅");
session.save(user2);
User user3=new User();
user3.setName("阿杜");
session.save(user3);
Role r1=new Role();
r1.setName("大堂经理");
Set users1=new HashSet();
users1.add(user1);
users1.add(user2);
r1.setUsers(users1);
session.save(r1);
Role r2=new Role();
r2.setName("出纳");
Set users2=new HashSet();
users2.add(user2);
users2.add(user3);
r2.setUsers(users2);
session.save(r2);
Role r3=new Role();
r3.setName("业务员");
Set users3=new HashSet();
users3.add(user1);
users3.add(user2);
users3.add(user3);
r3.setUsers(users3);
session.save(r3);
tx.commit();
}catch(Exception ex){
ex.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
public void testLoad1(){
Session session=null;
Transaction tx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
User user=(User)session.load(User.class, 3);
System.out.println(user.getName());
for(Iterator it=user.getRoles().iterator();it.hasNext();){
Role role=(Role)it.next();
System.out.println(role.getName());
}
tx.commit();
}catch(Exception ex){
ex.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
public void testLoad2(){
Session session=null;
Transaction tx=null;
try{
session=HibernateUtils.getSession();
tx=session.beginTransaction();
Role role=(Role)session.load(Role.class, 3);
System.out.println(role.getName());
for(Iterator it=role.getUsers().iterator();it.hasNext();){
User user=(User)it.next();
System.out.println(user.getName());
}
tx.commit();
}catch(Exception ex){
ex.printStackTrace();
tx.rollback();
}finally{
HibernateUtils.closeSession(session);
}
}
}