一、多对多配置
User.java
/**
* Created by Donald on 2016/11/20.
*/
public class User {
private Integer user_id; //用户id
private String user_name; //用户名
private String user_password; //用户密码
//一个用户可以有多个角色
private Set<Role> setRole = new HashSet<Role>();
public Set<Role> getSetRole() {
return setRole;
}
public void setSetRole(Set<Role> setRole) {
this.setRole = setRole;
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
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;
}
}
Role.java
/**
* Created by Donald on 2016/11/20.
*/
public class Role {
private Integer role_id; //角色id
private String role_name; //角色名称
private String role_memo; //角色描述
//角色表示多个用户
private Set<User> setUser = new HashSet<User>();
public Set<User> getSetUser() {
return setUser;
}
public void setSetUser(Set<User> setUser) {
this.setUser = setUser;
}
public Integer getRole_id() {
return role_id;
}
public void setRole_id(Integer 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;
}
}
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 1.配置类和表对应
class标签
name属性,实体类
table属性,数据表名称
-->
<class name="com.yyf.manytomany.User" table="t_user">
<id name="user_id" column="user_id">
<generator class="native"></generator>
</id>
<property name="user_name" column="user_name" ></property>
<property name="user_password" column="user_password"></property>
<set name="setRole" table="user_role">
<key column="userid"></key>
<!--
class,角色实体类全路径
column,角色在第三张表外键名称
-->
<many-to-many class="com.yyf.manytomany.Role" column="roleid"/>
</set>
</class>
</hibernate-mapping>
Role.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 1.配置类和表对应
class标签
name属性,实体类
table属性,数据表名称
-->
<class name="com.yyf.manytomany.Role" table="t_Role">
<id name="role_id" column="role_id">
<generator class="native"></generator>
</id>
<property name="role_name" column="role_name" ></property>
<property name="role_memo" column="role_memo"></property>
<set name="setUser" table="user_role">
<key column="roleid"></key>
<many-to-many class="com.yyf.manytomany.User" column="userid"/>
</set>
</class>
</hibernate-mapping>
二、级联操作—-保存
若无save-update则保存不成功。
测试:
/**
* 添加两个用户,为每个用户添加两个角色
*/
@Test
public void testSave() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
User user1 = new User();
user1.setUser_name("lucy");
user1.setUser_password("123");
User user2 = new User();
user2.setUser_name("mary");
user2.setUser_password("456");
Role role1 = new Role();
role1.setRole_name("总经理");
role1.setRole_memo("总经理");
Role role2 = new Role();
role2.setRole_name("秘书");
role2.setRole_memo("秘书");
Role role3 = new Role();
role3.setRole_name("保安");
role3.setRole_memo("保安");
//2. 建立关系,把角色放到用户里面
//user1 ----r1/r2
user1.getSetRole().add(role1);
user1.getSetRole().add(role2);
//user2 ----r2/r3
user2.getSetRole().add(role2);
user2.getSetRole().add(role3);
//3.保存用户
session.save(user1);
session.save(user2);
tx.commit();
}catch (Exception e){
tx.rollback();
}
}
三、级联操作—-删除
这种删除会将关联的所有的数据删除
@Test
public void testDelete2() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
User user = session.get(User.class, 3);
session.delete(user);
tx.commit();
}catch (Exception e){
tx.rollback();
}
}
四、级联操作—-维护第三张表
其实主要操作set
/**
* 1.用户和角色多对多关系,维护关系通过第三张表维护
*
* 2.让某个用户有某个角色
* ① 根据id查询用户和角色
* ②把角色对象放到用户set集合
*
* 3.让某个用户没有某个角色
* ①根据id查询用户和角色
* ②从用户里面把角色去掉
*
*/
@Test
public void testOperator() {
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
/**
* 让某个用户有某个角色
* 让lucy有经纪人角色
*
* 1.查询lucy和经济人
* 2.放入user的set里
*/
// User lucy = session.get(User.class, 1);
// Role role = session.get(Role.class, 1);
//
// lucy.getSetRole().add(role);
/**
* 删除操作
*/
User user = session.get(User.class, 4);
Role role = session.get(Role.class, 3);
user.getSetRole().remove(role);
tx.commit();
}catch (Exception e){
tx.rollback();
}
}