Eugenio Cuevas
Eugenio Cuevas

Reputation: 11078

Wrong update while cascading many-to-many relationship

I have a many-to-many relationship defined on hibernate like this:

User

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;
    }

}

So a user has a list of UserCustomer, that represent roles of users over customers. The problem is, that when we change a role over a customer and call update(), instead of one row updated we get all the rows updated with the same role. When we call merge() it starts to perform a lots of queries and then gives stackoverflow exception ¿Could this be a mapping problem?

Upvotes: 4

Views: 258

Answers (1)

Divanshu
Divanshu

Reputation: 1907

Can you post the tables and updation code?

I think you are updating the role directly from UserCustomer which should be updating all the roles, as far as my understanding goes you do not want to update UserCustomer but only RoleCustomer. Try to fetch RoleCustomer and update it not the UserCustomer.

Upvotes: 1

Related Questions