错误的更新,同时级联多对多关系

时间:2012-11-27 15:13:03

标签: java hibernate orm mapping

我在hibernate上定义了多对多关系,如下所示:

用户

public class User{

private List<UserCustomer> userCustomerList;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "id.user", cascade=CascadeType.ALL)
    public List<UserCustomer> getUserCustomerList() {
        return userCustomerList;
    }
}

UserCustomer

@Entity
@Table(name = "RTDB_USER_CUSTOMER")
@Component("userCustomerEntity")
@AssociationOverrides({
    @AssociationOverride(name = "id.user", 
            joinColumns = @JoinColumn(name = "ID_USER")),
    @AssociationOverride(name = "id.customer", 
            joinColumns = @JoinColumn(name = "ID_CUSTOMER")) })

public class UserCustomer {

    @EmbeddedId
    public UserCustomerId getId() {
        return id;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumns({ @JoinColumn(name = "ID_ROLE_CUSTOMER", referencedColumnName = "ID") })           public RoleCustomer getRoleCustomer() {
        return roleCustomer;
    }

}

因此,用户有一个UserCustomer列表,表示用户对客户的角色。问题是,当我们更改客户的角色并调用update()时,而不是更新一行,我们会使用相同的角色更新所有行。当我们调用merge()时,它会开始执行大量查询,然后给出stackoverflow异常¿这可能是映射问题吗?

1 个答案:

答案 0 :(得分:1)

您可以发布表格和更新代码吗?

我认为您正在直接从UserCustomer更新角色,该角色应该更新所有角色,据我所知,您不想更新UserCustomer而只更新RoleCustomer。尝试抓取RoleCustomer并更新UserCustomer