JPA: why merge will update join table without cascade

776 Views Asked by At

Thanks for taking a look at this question in advance.

Basically, I have a few entities and here's the relationship.

Table A ---< Table AB >---- Table B

And the entity classes:

class A {    
     @ManyToMany
     @JoinTable(name = "A_B", joinColumns = { @JoinColumn(name = "A_ID",nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "B_ID", nullable = false) })
     private List<B> bList;
 }

class B {    
    @ManyToMany(mappedBy = "bList")
    private List<A> aList;
}

Now I have 2 sessions accessing the same A object. I changed the bList value of A and saved it in session 1. So now, the object A in session 2 is stale and I saved an object C, which has reference of object A in its stale state.

I wonder why WITHOUT using cascade = {PERSIST, MERGE} by putting it into the @ManyToMany annotations, saving object C would also update the stale state of A back into the database, so the changes I made about object A in session 1 is gone?

0

There are 0 best solutions below