Johnson Eyo
Johnson Eyo

Reputation: 113

jpa many to many inverse side cant perform merge operation after been cascaded

i have two entity classes with mapping @ManytoMany

Inverse Side
public class Users implements Serializable {
@ManyToMany(mappedBy = "usersList",cascade=CascadeType.MERGE)
private List<Tags> tagsList = new ArrayList();

Owning Side
public class Tags implements Serializable {
 @JoinTable(name = "USERS_TAG_XREF", joinColumns = {
    @JoinColumn(name = "TAG_ID", referencedColumnName = "TAG_ID")}, inverseJoinColumns  = {
    @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")})
  @ManyToMany
  private List<Users> usersList = new ArrayList();

Now when i perform an operation on the owning side like this;

 for(Tags t : tList){
 Users u = em.find(Users.class, id);  
 t.getUsersList().clear();
 em.merge(t);
 }

It clears many users U relations with the Tag entity t, i assume this works because Tag entity is the owing side , but when i perform this with the inverse side ;

 Users u = em.find(Users.class,id);
 u.getTagList().clear();
 em.merge(u);

It doesnt do anything on the table, i actually want the code to clear many Tags t relations with one User u, but i feel since this is an inverse side by adding the cascade=CascadeType.MERGE would allow merge operation as this em.merge(u); but it doesnt please help

Upvotes: 0

Views: 494

Answers (1)

Ilya
Ilya

Reputation: 29693

You should also remove User from Tag entity

Users u = em.find(Users.class,id);
{
    for (Tag t : u.getTagList())
    {
       t.hetUserList().remove(u);
    }
    u.getTagList().clear();
    em.merge(u);
}

Upvotes: 2

Related Questions