一、使用用户User和Role实现多对多的示例
User.java,实现对Role的引用
package com.lxit.entity; import java.util.HashSet; import java.util.Set; public class User { public User(){ } public User(String userid, String userName, String userPwd) { this.userid = userid; this.userName = userName; this.userPwd = userPwd; } private String userid; private String userName; private String userPwd; private Set<Role> roles = new HashSet<Role>(); public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
User.hbm.xml配置文件,映射一个多对多的集合,并开启级联效果,由User来维护级联效果
<?xml version="1.0" encoding="UTF-8"?> <!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.lxit.entity.User" table="User"> <id name="userid" type="java.lang.String"> <generator class="assigned" /> </id> <property generated="never" lazy="false" name="userName" type="java.lang.String"> <column length="20" name="userName" not-null="true" /> </property> <property generated="never" lazy="false" name="userPwd" type="java.lang.String"> <column length="20" name="userPwd" not-null="true" /> </property> <set name="roles" table="user_role" cascade="all"> <key column="userid" /> <many-to-many column="roleid" class="com.lxit.entity.Role" /> </set> </class> </hibernate-mapping>
Role.java 角色类,同样也映射Users集合
package com.lxit.entity; import java.util.HashSet; import java.util.Set; public class Role { public Role() { } public Role(String roleName, String roleDesc) { this.roleName = roleName; this.roleDesc = roleDesc; } private int roleId; private String roleName; private String roleDesc; private Set<User> users = new HashSet<User>(); public int getRoleId() { return roleId; } public void setRoleId(int roleId) { this.roleId = roleId; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getRoleDesc() { return roleDesc; } public void setRoleDesc(String roleDesc) { this.roleDesc = roleDesc; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
User.hbm.xml映射文件,映射User多对多关系,并设置inverse="true"。
<?xml version="1.0" encoding="UTF-8"?> <!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.lxit.entity.Role" table="role"> <id name="roleId" type="java.lang.Integer"> <generator class="native"></generator> </id> <property name="roleName" type="java.lang.String"> <column name="roleName" length="20" not-null="true"></column> </property> <set name="users" table="user_role" inverse="true"> <key column="roleid"></key> <many-to-many column="userid" class="com.lxit.entity.User" /> </set> </class> </hibernate-mapping>
添加Mapping映射文件
<mapping resource="com/lxit/entity/Role.hbm.xml" /> <mapping resource="com/lxit/entity/User.hbm.xml" />
最后测试类
public static void main(String[] args) { Role sys = new Role("admin", "系統管理员"); Role teacher = new Role("teacher","教师"); User user1 = new User("admin", "管理员", "123123"); User user2 = new User("zhangsan", "张三疯", "123123"); user1.getRoles().add(sys); user1.getRoles().add(teacher); user2.getRoles().add(teacher); Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); //session.save(sys); //session.save(teacher); session.save(user1); session.save(user2); tx.commit(); }
以上,只需要将role添加到user就会自动生成多对多的权限,多对多必须使用第三张表来实现权限效果。不能为在第三张表添加映射文件。