表中的关系:
实体中的关系:
员工类User.java
package cn.future.domain; import java.util.HashSet; import java.util.Set; public class User { /*CREATE TABLE `sys_user` ( `user_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '用户id', `user_code` varchar(32) NOT NULL COMMENT '用户账号', `user_name` varchar(64) NOT NULL COMMENT '用户名称', `user_password` varchar(32) NOT NULL COMMENT '用户密码', `user_state` char(1) NOT NULL COMMENT '1:正常,0:暂停', PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;*/ private Long user_id; private String user_code; private String user_name; private String user_password; private Character user_state; private Set<Role> roles = new HashSet<Role>(); public Long getUser_id() { return user_id; } public void setUser_id(Long user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_password() { return user_password; } public void setUser_password(String user_password) { this.user_password = user_password; } public Character getUser_state() { return user_state; } public void setUser_state(Character user_state) { this.user_state = user_state; } public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } }
员工表映射文件User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.future.domain" > <class name="User" table="sys_user" > <id name="user_id" > <generator class="native"></generator> </id> <property name="user_code" column="user_code" ></property> <property name="user_name" column="user_name" ></property> <property name="user_password" column="user_password" ></property> <property name="user_state" column="user_state" ></property> <!-- 多对多关系 --> <!-- name:集合属性名 table:中间表名 key:column 外键,别人引用"我"的外键列名 many-to-many:我与哪个类是多对多的关系 column:外键,我引用别人的外键列名 --> <set name="roles" table="sys_user_role"> <key column="user_id"></key> <many-to-many class="Role" column="role_id"></many-to-many> </set> </class> </hibernate-mapping>
角色类Role.java
package cn.future.domain; import java.util.HashSet; import java.util.Set; public class Role { /*CREATE TABLE `sys_role` ( `role_id` bigint(32) NOT NULL AUTO_INCREMENT, `role_name` varchar(32) NOT NULL COMMENT '角色名称', `role_memo` varchar(128) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`role_id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;*/ private Long role_id; private String role_name; private String role_memo; private Set<User> users = new HashSet<User>(); public Long getRole_id() { return role_id; } public void setRole_id(Long role_id) { this.role_id = role_id; } public String getRole_name() { return role_name; } public void setRole_name(String role_name) { this.role_name = role_name; } public String getRole_memo() { return role_memo; } public void setRole_memo(String role_memo) { this.role_memo = role_memo; } public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } }
角色映射文件Role.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.future.domain" > <class name="Role" table="sys_role" > <id name="role_id" > <generator class="native"></generator> </id> <property name="role_name" column="role_name" ></property> <property name="role_memo" column="role_memo" ></property> <!-- inverse属性:配置关系是否维护 true:customer不维护关系 false(默认值):customer维护关系 多对多关系表中必须有一方设置这个属性为true --> <set name="users" inverse="true" table="sys_user_role"> <key column="role_id"></key> <many-to-many class="User" column="user_id"></many-to-many> </set> </class> </hibernate-mapping>
操作:
package cn.future.many2many; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.future.domain.Customer; import cn.future.domain.LinkMan; import cn.future.domain.Role; import cn.future.domain.User; import cn.future.utils.HibernateUtils; public class Demo { @Test //多对多一关系操作 public void fun(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //创建两个User User ms = new User(); ms.setUser_name("ms"); User grf = new User(); grf.setUser_name("grf"); //创建三个个Role Role home = new Role(); home.setRole_name("home"); Role wife = new Role(); wife.setRole_name("wife"); Role husband = new Role(); husband.setRole_name("husband"); //用户表达关系 ms.getRoles().add(home); ms.getRoles().add(husband); grf.getRoles().add(home); grf.getRoles().add(wife); //角色表达关系 如果某方设置invserse="true"时不用add来维护关系(下面四行代码可以去掉) /*home.getUsers().add(ms); home.getUsers().add(grf); wife.getUsers().add(grf); husband.getUsers().add(ms);*/ //保存 session.save(ms); session.save(grf); session.save(home); session.save(wife); session.save(husband); //4提交事务 ts.commit(); //5关闭资源 session.close(); } @Test //为用户添加角色 public void fun1(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //获得用户 User ms = session.get(User.class, 1l); //创建角色 Role man = new Role(); man.setRole_name("man"); //将角色添加到用户中 ms.getRoles().add(man); //将角色转换为持久化 session.save(man); //4提交事务 ts.commit(); //5关闭资源 session.close(); } @Test //为用户解除角色 public void fun2(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //获得用户 和 角色 User ms = session.get(User.class, 1l); Role man = session.get(Role.class, 4l); //将角色从用户集合中移除 ms.getRoles().remove(man); //4提交事务 ts.commit(); //5关闭资源 session.close(); } @Test //查看用户所有角色 public void fun3(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //获得用户 User ms = session.get(User.class, 1l); Set<Role> roles = ms.getRoles(); for(Role role : roles){ System.out.println(role.getRole_name()); } //4提交事务 ts.commit(); //5关闭资源 session.close(); } @Test //查看角色下所有用户 public void fun4(){ //1获得session Session session = HibernateUtils.openSession(); //2开启事务 Transaction ts = session.beginTransaction(); //3操作 //获得用户 Role home = session.get(Role.class, 1l); Set<User> users = home.getUsers(); for(User user : users){ System.out.println(user.getUser_name()); } //4提交事务 ts.commit(); //5关闭资源 session.close(); } }
注意:多对多关系,在维护关系式一定要设置某方放弃维护关系(也就是给某方设置inverse="true")