I. User and Role implement user-many examples
User.java, to achieve a reference to the Role of
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 configuration files, maps, a collection of many to many, and open the cascading effect, by the User to maintain the cascading effect
<?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 character class, is also mapping the Users collection
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 mapping file, mapping User-many relationship, and set the 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>
Add Mapping mapping file
<mapping resource="com/lxit/entity/Role.hbm.xml" /> <mapping resource="com/lxit/entity/User.hbm.xml" />
The last test class
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(); }
Above, only need to add the role to the user will automatically generate many to many privileges, you must use third-many table to achieve privilege effect. You can not add a map file in the third table.
I. User and Role implement user-many examples
User.java, to achieve a reference to the Role of
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 configuration files, maps, a collection of many to many, and open the cascading effect, by the User to maintain the cascading effect
<?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 character class, is also mapping the Users collection
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 mapping file, mapping User-many relationship, and set the 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>
Add Mapping mapping file
<mapping resource="com/lxit/entity/Role.hbm.xml" /> <mapping resource="com/lxit/entity/User.hbm.xml" />
The last test class
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(); }
Above, only need to add the role to the user will automatically generate many to many privileges, you must use third-many table to achieve privilege effect. You can not add a map file in the third table.