4

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

用户

public class User{

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

用户客户

@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 列表,代表用户对客户的角色。问题是,当我们将角色更改为 customer 并调用时update(),我们不是更新了一行,而是使用相同的角色更新了所有行。当我们调用merge()它时,它开始执行大量查询,然后给出stackoverflow异常¿这可能是映射问题吗?

4

1 回答 1

1

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

我认为您正在直接UserCustomer更新应该更新所有角色的角色,据我了解,您不想更新UserCustomer,而只想更新RoleCustomer. 尝试获取RoleCustomer和更新它而不是UserCustomer.

于 2012-12-05T12:24:16.307 回答