Hibernate的@OneToMany从列表更新时,父母子女删除 [英] Hibernate @OneToMany remove child from list when updating parent

查看:2280
本文介绍了Hibernate的@OneToMany从列表更新时,父母子女删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

团队

  @Entity
@表
公共类小组{
[..]
私人设置< UserTeamRole> userTeamRoles;/ **
 * @返回userTeamRoles
 * /
@OneToMany(级联= {} CascadeType.ALL,的mappedBy =团队,取= FetchType.LAZY)
公开组< UserTeamRole> getUserTeamRoles(){
    返回userTeamRoles;
}/ **
 * @参数userTeamRoles
 *在userTeamRoles设置
 * /
公共无效setUserTeamRoles(SET< UserTeamRole> userTeamRoles){
    this.userTeamRoles = userTeamRoles;
}

}

USER_TEAM_ROLE

  @Entity
@Table(NAME =user_team_role)
公共类UserTeamRole { @ManyToOne(级联= CascadeType.MERGE,取= FetchType.LAZY)
 @JoinColumn(NAME =FK_TeamId)
 公共队getTeam(){
    返回团队;
 }
}

现在,更新包含例如Team.userTeamRoles = {UTR1,UTR2}以{UTR1,UTR3}团队实体的时候,我想UTR2被删除。但是我现在做的方式,旧列表保持不变,只增加了UTR3到列表中。

这是我如何做到这一点的时刻:

 如果(!usersDualListData.getTarget()的isEmpty()){
        //为球队中的每个用户的角色将是雇员
        team.setUserTeamRoles(新的HashSet&所述; UserTeamRole>());
        角色roleForUser = roleService
                .getRoleByName(RoleNames.ROLE_EMPLOYEE.name());
        对于(用户用户:usersDualListData.getTarget()){
            UserTeamRole UTR =新UserTeamRole();
            utr.setUser(用户);
            utr.setTeam(队);
            utr.setRole(roleForUser);
            。team.getUserTeamRoles()加(UTR);
        }
    }teamService.updateTeam(队);

我觉得做 team.setUserTeamRoles(新的HashSet< UserTeamRole>()); 清单将被重置,因为级联的previous名单将被删除。

任何帮助是AP preciated。谢谢


解决方案

  1. 而不是取代的集合( team.setUserTeamRoles(新的HashSet< UserTeamRole>()); ),你必须明确( )现有之一。这是因为,如果从休眠DB加载实体(及其集合),其管理起来,即得。跟踪它们的变化。一般使用Hibernate的时候,最好的不可以创建集合(列表,集合)的制定者。创建只有吸气,并清除被它返回的集合,即:

    team.getUserTeamRoles()清();


  2. 另一件事是,你错过了孤儿缺失(即当它从集合中删除父删除子对象)。要启用它,你需要添加 @OneToMany(orphanRemoval = TRUE)在所属的实体。


I have the following entities:

TEAM

@Entity
@Table
public class Team {
[..]
private Set<UserTeamRole> userTeamRoles;

/**
 * @return the userTeamRoles
 */
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "team", fetch = FetchType.LAZY)
public Set<UserTeamRole> getUserTeamRoles() {
    return userTeamRoles;
}

/**
 * @param userTeamRoles
 *            the userTeamRoles to set
 */
public void setUserTeamRoles(Set<UserTeamRole> userTeamRoles) {
    this.userTeamRoles = userTeamRoles;
}

}

and

USER_TEAM_ROLE

@Entity
@Table(name = "user_team_role")
public class UserTeamRole {

 @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
 @JoinColumn(name = "FK_TeamId")
 public Team getTeam() {
    return team;
 }
}

Now, when updating a Team entity that contains for example Team.userTeamRoles = {UTR1, UTR2} with {UTR1, UTR3}, I want UTR2 to be deleted. But the way I do it now, the old list remains the same and it only adds UTR3 to the list.

This is how I do it at the moment:

 if (!usersDualListData.getTarget().isEmpty()) {
        // the role for each user within the team will be "employee"
        team.setUserTeamRoles(new HashSet<UserTeamRole>());
        Role roleForUser = roleService
                .getRoleByName(RoleNames.ROLE_EMPLOYEE.name());
        for (User user : usersDualListData.getTarget()) {
            UserTeamRole utr = new UserTeamRole();
            utr.setUser(user);
            utr.setTeam(team);
            utr.setRole(roleForUser);
            team.getUserTeamRoles().add(utr);
        }
    }

teamService.updateTeam(team);

I thought that by doing team.setUserTeamRoles(new HashSet<UserTeamRole>()); the list would be reset and because of the cascades the previous list would be deleted.

Any help is appreciated. Thank you

解决方案

  1. Instead of replacing the collection (team.setUserTeamRoles(new HashSet<UserTeamRole>());) you have to clear() the existing one. This happens because if Hibernate loads the entity (and its collections) from DB, it "manages" them, ie. tracks their changes. Generally when using Hibernate it's better not to create any setters for collections (lists, sets). Create only the getter, and clear the collection returned by it, ie:

    team.getUserTeamRoles().clear();

  2. Another thing is that you miss orphan deletion (ie. delete child object when it's removed from collection in the parent). To enable it, you need to add @OneToMany(orphanRemoval=true) in owning entity.

这篇关于Hibernate的@OneToMany从列表更新时,父母子女删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆