本来多对多(用户角色)会产生第3个中间表(用户角色表),现在把中间表单独抽象出一个类来,然后用它分别和用户表多对一,角色表多对一。
用户表类:
package com.bjpowernode.hibernate.domain;
public class User {
private int id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
角色表类:
package com.bjpowernode.hibernate.domain;
public class Role {
private int id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
角色用户类:
package com.bjpowernode.hibernate.domain;
public class UserRole {
private int id;
private User user;
private Role role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
用户.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.bjpowernode.hibernate.domain.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
角色.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.bjpowernode.hibernate.domain.Role" table="t_role"> <id name="id"> <generator class="native"/> </id> <property name="name"/> </class> </hibernate-mapping>
角色用户.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.bjpowernode.hibernate.domain.UserRole" table="t_user_role"> <id name="id"> <generator class="native"/> </id> <many-to-one name="user" column="userId"/> <many-to-one name="role" column="roleId"/> </class> </hibernate-mapping>
单元测试:
public void testSave2(){
Session session = null;
try {
session = HibernateUtils.createSession();
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);
//role到此结束
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);
//user到此结束
//张三,数据录入人员
UserRole ur1=new UserRole();
ur1.setRole(role1);
ur1.setUser(user1);
session.save(ur1);
//张三,商务主管
UserRole ur2=new UserRole();
ur2.setRole(role2);
ur2.setUser(user1);
session.save(ur2);
//张三,商务经理
UserRole ur3=new UserRole();
ur3.setRole(role3);
ur3.setUser(user1);
session.save(ur3);
//李四,数据录入人员
UserRole ur4_1=new UserRole();
ur4_1.setRole(role1);
ur4_1.setUser(user2);
session.save(ur4_1);
//李四,商务主管
UserRole ur4_2=new UserRole();
ur4_2.setRole(role2);
ur4_2.setUser(user2);
session.save(ur4_2);
//王五,商务经理
UserRole ur5_1=new UserRole();
ur5_1.setRole(role3);
ur5_1.setUser(user3);
session.save(ur5_1);
//王五,商务会计
UserRole ur5_2=new UserRole();
ur5_2.setRole(role4);
ur5_2.setUser(user3);
session.save(ur5_2);
session.getTransaction().commit();
} catch (Exception ex) {
ex.printStackTrace();
session.getTransaction().rollback();
} finally {
HibernateUtils.closeSession(session);
}
refurl:
http://blog.csdn.net/yixiu285/article/details/7749109
http://www.cnblogs.com/syxchina/archive/2012/04/09/2439280.html