Hibernate--多对多关系的表达、配置与操作(保存、修改、删除)详解
程序员文章站
2022-04-24 10:29:04
...
创建表
创建sys_user、sys_role、sys_user_role三张表:
创建实体
User:
public class User {
//主键
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;
}
}
Role:
//角色对象
public class Role {
//主键
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;
}
}
配置映射关系
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="pers.zhang.domain" >
<class name="User" table="sys_user" >
<id name="user_id" >
<generator class="native"></generator>
</id>
<property name="user_code" ></property>
<property name="user_name" ></property>
<property name="user_password" ></property>
<property name="user_state" ></property>
<!-- 多对多关系表达 -->
<!--
name: 集合属性名
table: 配置中间表名
key:
column:外键,别人引用"我"的外键列名
class: 我与哪个类是多对多关系
column:外键.我引用比人的外键列名
-->
<!-- cascade级联操作:
save-update: 级联保存更新
delete:级联删除
all:级联保存更新+级联删除
结论: cascade简化代码书写.该属性使不使用无所谓. 建议要用只用save-update.
如果使用delete操作太过危险.尤其在多对多中.不建议使用.
-->
<set name="roles" table="sys_user_role" cascade="save-update" >
<key column="user_id" ></key>
<many-to-many class="Role" column="role_id" ></many-to-many>
</set>
</class>
</hibernate-mapping>
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="pers.zhang.domain" >
<class name="Role" table="sys_role" >
<id name="role_id" >
<generator class="native"></generator>
</id>
<property name="role_name" ></property>
<property name="role_memo" ></property>
<!-- 使用inverse属性
true: 放弃维护外键关系
false(默认值):维护关系
结论: 将来在开发中,如果遇到多对多关系.一定要选择一方放弃维护关系.
一般谁来放弃要看业务方向. 例如录入员工时,需要为员工指定所属角色.
那么业务方向就是由员工维护角色. 角色不需要维护与员工关系.角色放弃维护
-->
<set name="users" table="sys_user_role" inverse="true" >
<key column="role_id" ></key>
<many-to-many class="User" column="user_id" ></many-to-many>
</set>
</class>
</hibernate-mapping>
操作
保存:
@Test
//保存员工以及角色
public void fun1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//-------------------------------------------------
//创建两个 User
User u1 = new User();
u1.setUser_name("小明");
User u2 = new User();
u2.setUser_name("小黑");
//创建两个 Role
Role r1 = new Role();
r1.setRole_name("保洁");
Role r2 = new Role();
r2.setRole_name("保安");
//用户表达关系
u1.getRoles().add(r1);
u1.getRoles().add(r2);
u2.getRoles().add(r1);
u2.getRoles().add(r2);
//角色表达关系
r1.getUsers().add(u1);
r1.getUsers().add(u2);
r2.getUsers().add(u1);
r2.getUsers().add(u2);
//调用Save方法一次保存
session.save(u1);
session.save(u2);
session.save(r1);
session.save(r2);
//-------------------------------------------------
tx.commit();
session.close();
}
更新:
@Test
//为小明新增一个角色
public void fun3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//-------------------------------------------------
//获得用户,小明
User user = session.get(User.class, 4l);
//创建角色
Role r = new Role();
r.setRole_name("食堂阿姨");
//将角色添加到用户中
user.getRoles().add(r);
//将角色转换为持久化
session.save(r);
//-------------------------------------------------
tx.commit();
session.close();
}
删除:
@Test
//为小明解除角色
public void fun4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//-------------------------------------------------
//获得用户,小明
User user = session.get(User.class, 4l);
//获得要操作的角色对象(保洁,保安)
Role r1 = session.get(Role.class, 3l);
Role r2 = session.get(Role.class, 4l);
//将角色从用户的角色集合中移除
user.getRoles().remove(r1);
user.getRoles().remove(r2);
//-------------------------------------------------
tx.commit();
session.close();
}
上一篇: 孤亭诗雨