当单向多对多,单向一对多,删除其中没有维护的一方的数据时,因为没有级联关系,需要手动取消关联关系,然后才能删除,不然因为外键报错
BUser-单向多对多->Role
BUser
package com.cpic.shz.entity;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.cpic.shz.util.DataUtil;
import com.google.common.collect.Sets;
/**
* @package : com.cpic.shz.entity
* @file : BUser.java
* @date : 2015年9月17日 上午10:47:25
* @user : liuhao
* @note : 后台角色表
*/
@Entity
@Table
public class BUser{
private static final long serialVersionUID = 1L;
private Integer id;
private Set<Role> roles = Sets.newHashSet();
public BUser() {
}
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToMany
@JoinTable(name = "shz_hc_buser_role",
joinColumns = { @JoinColumn(name = "buser_id") },
inverseJoinColumns = { @JoinColumn(name = "role_id") })
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
Role
package com.cpic.shz.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ToStringBuilder;
@Entity
@Table
public class Role {
private static final long serialVersionUID = 1L;
private Integer id;
private Set<Right> rights = new HashSet<Right>();
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
当删除一个role时,因为buser_role表中外键连接两个buser_id,role_id,role又没有级联关系,所以报错
手动取消buser_role表中带有当前role_id的行
再删除role
deleteRole方法
public String deleteRole() {
try {
buserService.updateBUserByRoleId(model.getId());
roleService.deleteEntity(model);
root.put("msg", "删除id=" + model.getId() + "成功!");
} catch (Exception e) {
root.put("msg", "删除id=" + model.getId() + "失败!");
e.printStackTrace();
}
return "json";
}
updateBUserByRoleId取消buser_role表中带有role_id的行
1. 多对多查询 select a from A a left join a.b where b.id = ?
2. 遍历busers中存在role_id的buser,将其移除
3. 保存新的buser
@SuppressWarnings("unchecked")
@Rollback(value=true)
@Override
public void updateBUserByRoleId(Integer id) {
StringBuffer hql = new StringBuffer();
hql.append("select buser from BUser buser left join buser.roles r ");
hql.append(" where r.id = ? ");
List<BUser> busers = findEntityByHQL(hql.toString(), id);
if(ObjectUtil.isNotEmpty(busers))
for (BUser buser : busers) {
Set<Role> roles = buser.getRoles();
List<Role> lroles = (List<Role>) ObjectUtil.changeSetToList(roles);
for (Role role : lroles) {
if (role.getId() == id)
roles.remove(role);
}
buser.setRoles(roles);
updateEntity(buser);
}
}
添加问题
eg:一对多添加数据不能在页面中直接buser.role.id,buser.role.name添加
只能将role_id一起传到后台,然后获取Integer[] role_id;的Role 对象
Set<Role> sets = buser.getRoles()
sets.add(role...);
buser.setRoles(sets);
saveOrUpdateEntity(buser);
public String saveOrUpdateRole() {
try {
List<Right> rights = rightService.findRightInRange(rightsId);
roleService.saveOrUpdateRole(model, rights);
root.put("msg", "操作" + model.getRoleName() + "成功!");
} catch (Exception e) {
root.put("msg", "操作" + model.getRoleName() + "失败!");
e.printStackTrace();
}
return "json";
}
查询所有的rightIds
@Override
public List<Right> findRightInRange(Integer[] rightsId) {
String hql = "from Right r where r.id in("+StringUtil.arr2Str(rightsId)+")";
return findEntityByHQL(hql);
}
设置添加Model
@Override
public void saveOrUpdateRole(Role model, List<Right> rights) {
if(model!=null){
HashSet<Right> setrights = (HashSet<Right>) model.getRights();
for(Right r:rights){
setrights.add(r);
}
model.setRights(setrights);
saveOrUpdateEntity(model);
}
}