Hibernate之多对多配置
程序员文章站
2022-04-24 10:29:22
...
在实际开发中,多对多的配置情况很少,一般都将多对多的情况拆分为两种一对多的情况,在此只做简单的配置。
以角色Role和用户User为例。
1,实体类配置
User:
package com.myself.domain;
import java.util.HashSet;
import java.util.Set;
public class User {
private Long id;
private String userCode;
private String userName;
private String userPassword;
private String userStatus;
private Set<Role> roles = new HashSet<Role>();
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStatus(String userStatus) {
this.userStatus = userStatus;
}
}
Role:
package com.myself.domain;
import java.util.HashSet;
import java.util.Set;
public class Role {
private Long id;
private String roleName;
private String roleMemo;
private Set<User> users = new HashSet<User>();
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleMemo() {
return roleMemo;
}
public void setRoleMemo(String roleMemo) {
this.roleMemo = roleMemo;
}
}
2,映射文件配置
User.hbm.xml:
<set name="roles" table="sys_user_role" inverse="true">
<key column="user_id"></key>
<many-to-many class="com.myself.domain.Role" column="role_id"></many-to-many>
</set>
注意:1,set标签上必须配置table属性,该属性为多对多两张表的中间表;
2,inverse=“true” 放弃外键维护权,在进行多对多配置的时候必须有一方要配置该属性,也就是必须有一方要放弃对外键的维护权;
3,<many-to-many> 用来指定当前映射文件对应的实体类和集合元素对应的实体类属于多对多的关系。
class:用来指定集合元素所对应的实体类;
column:用来指定集合元素对应表在中间表中外键的名称
Role.hbm.xml:
<set name="users" table="sys_user_role">
<key column="role_id"></key>
<many-to-many class="com.myself.domain.User" column="user_id"></many-to-many>
</set>
多对多双方的配置基本差不多,唯一区别在于必须有一方需要放弃对外键的维护权。在多对多的关系中,双方由于属于平级关系,双方并无依赖对方的关系存在,所以一般不配置级联操作。
3,测试
package com.myself.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.myself.domain.Role;
import com.myself.domain.User;
import com.myself.utils.HibernateUtils;
public class ManyToManyTest {
@Test
public void test(){
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
User user1 = new User();
User user2 = new User();
User user3 = new User();
user1.setUserName("testUser001");
user2.setUserName("testUser002");
user3.setUserName("testUser003");
Role role1 = new Role();
Role role2 = new Role();
role1.setRoleName("testRole001");
role2.setRoleName("testRole002");
user1.getRoles().add(role1);
user1.getRoles().add(role2);
user2.getRoles().add(role1);
user3.getRoles().add(role2);
role1.getUsers().add(user1);
role1.getUsers().add(user2);
role2.getUsers().add(user1);
role2.getUsers().add(user3);
session.save(role1);
session.save(role2);
session.save(user1);
session.save(user2);
session.save(user3);
transaction.commit();
session.close();
}
}