penweizgx
penweizgx
2014-07-11 00:09

Hibernate4多对多中间表维护?

已采纳

角色role和权限perm是多对多关系,在中间表t_role_perm中有一个角色对应多个权限,我现在想取消几个权限,一般的数据库方式就是 delete from t_role_perm where role_id=? and perm_id=?就可以了,但是hibernate怎么才能达到这样的效果?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • gc00001 gc00001 7年前

    这得看你是怎么写hibernate的pojo了,如果是像一楼那样子把中间表也生成出来那么一楼那样子直接getSession().delete(RoleFunctionEntity)删除中间表中的数据就行了;如果你用hibernate反向工程生成pojo并且选择了enable many to many detect,那么不生成中间表的类, 那么你得先看看是哪一方在维护中间表,只要看mappedby加在哪一方那么那一方就不维护中间表,假如你这里role是维护中间表的,那么你先session.get(role.class,id)这样子拿出你这个角色对象role,然后你role.getPerms拿出这个权限的角色set集合,然后想去掉哪个权限就删除这个集合里相应的权限类就ok了,最后直接session.update(role)就行了 我说的是注解配置的hibernate你要是xml配置的我们再讨论

    点赞 评论 复制链接分享
  • tank2140896 tank2140896 7年前

    我之前也做过类似的,我们是role、function、及中间表rolefunction
    [code="java"]
    @Entity
    @Table(name="roletbl")
    public class Role implements Serializable{

    private static final long serialVersionUID = -4758237150336472776L;
    
    @Id
    @Column(name = "iRoleId", nullable = false)
    private Integer iRoleId;
    
    @Column(name = "sRoleName", nullable = false, unique = true, length = 32)
    private String sRoleName;
    
    @Column(name = "sRoleDetails", nullable = true, length = 128)
    private String sRoleDetails;
    
    @Column(name = "iRoleMode", nullable = false)
    private Integer iRoleMode;
    
    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name = "iRoleId")
    private List<RoleFunction> rftb = new ArrayList<RoleFunction>();
    
    public List<RoleFunction> getRftb() {
        return rftb;
    }
    
    public void setRftb(List<RoleFunction> rftb) {
        for (int i = 0; i < rftb.size(); i++) {
            rftb.get(i).setiRoleId(iRoleId);
        }
        this.rftb = rftb;
    }
    
    public Integer getiRoleId() {
        return iRoleId;
    }
    
    public void setiRoleId(Integer iRoleId) {
        this.iRoleId = iRoleId;
    }
    
    public String getsRoleName() {
        return sRoleName;
    }
    
    public void setsRoleName(String sRoleName) {
        this.sRoleName = sRoleName;
    }
    
    public String getsRoleDetails() {
        return sRoleDetails;
    }
    
    public void setsRoleDetails(String sRoleDetails) {
        this.sRoleDetails = sRoleDetails;
    }
    
    public Integer getiRoleMode() {
        return iRoleMode;
    }
    
    public void setiRoleMode(Integer iRoleMode) {
        this.iRoleMode = iRoleMode;
    }
    

    }
    [/code]
    [code="java"]
    @Entity
    @Table(name="functiontbl")
    public class Function implements Serializable{

    private static final long serialVersionUID = -1207218576602710544L;
    
    @Id
    @Column(name = "iFunctionId", nullable = false)
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer iFunctionId;
    
    @Column(name = "sFunctionName", nullable = false, unique = true, length = 32)
    private String sFunctionName;
    
    @Column(name = "sFunctionDetails", nullable = true, length = 128)
    private String sFunctionDetails;
    
    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumn(name = "iFunctionId")
    private List<RoleFunction> rftb;
    
    public Integer getiFunctionId() {
        return iFunctionId;
    }
    
    public void setiFunctionId(Integer iFunctionId) {
        this.iFunctionId = iFunctionId;
    }
    
    public String getsFunctionName() {
        return sFunctionName;
    }
    
    public void setsFunctionName(String sFunctionName) {
        this.sFunctionName = sFunctionName;
    }
    
    public String getsFunctionDetails() {
        return sFunctionDetails;
    }
    
    public void setsFunctionDetails(String sFunctionDetails) {
        this.sFunctionDetails = sFunctionDetails;
    }
    
    public List<RoleFunction> getRftb() {
        return rftb;
    }
    
    public void setRftb(List<RoleFunction> rftb) {
        this.rftb = rftb;
    }
    

    }
    [/code]
    [code="java"]
    @Entity
    @Table(name="rolefunctiontbl")
    @IdClass(value=RoleFunctionPK.class)
    public class RoleFunction implements Serializable{

    private static final long serialVersionUID = -3218895861455447832L;
    
    @Transient
    private Integer iRoleId;
    
    private Integer iFunctionId;
    
    @Column(name = "iFunctionAuth", nullable = true)
    private Integer iFunctionAuth;
    
    @ManyToOne(cascade=CascadeType.ALL,optional=false)
    @JoinColumn(name = "iRoleId")
    private Role roleTbl;
    
    @ManyToOne(cascade=CascadeType.ALL,optional=false)
    @JoinColumn(name = "iFunctionId")
    private Function functionTbl;
    
    @Id
    public Integer getiRoleId() {
        return iRoleId;
    }
    
    public void setiRoleId(Integer iRoleId) {
        this.iRoleId = iRoleId;
    }
    
    @Id
    public Integer getiFunctionId() {
        return iFunctionId;
    }
    
    public void setiFunctionId(Integer iFunctionId) {
        this.iFunctionId = iFunctionId;
    }
    
    public Integer getiFunctionAuth() {
        return iFunctionAuth;
    }
    
    public void setiFunctionAuth(Integer iFunctionAuth) {
        this.iFunctionAuth = iFunctionAuth;
    }
    

    }
    [/code]

    hibernate大致可以这么写:
    getSession().delete(RoleFunctionEntity);
    但是不知道你的逻辑是什么?只删中间关系表就可以?还是要级联删除?

    点赞 评论 复制链接分享
  • leehuat leehuat 7年前

    [code="java"]
    不需要这样删除吧, 可以删除对象的方式吧
    roleEntity.getUserGroups().clear();
    session.delete(roleEntity);
    [/code]

    点赞 评论 复制链接分享

相关推荐