3

Following situation: Using EJB on Glassfish, I have two entity classes: A and B. They are linked by a @ManyToMany relationship so I have an additional table A_B.

Now I'd like to work on an instance of A, i.e. also change some relations to class B. More precisely, I alter values in A from time to time, later (and in a different invocation of a Bean's method from the client) I want to submit the changes.

Therefor, I thought of using an optimistic lock on A's instance. The problem is: as the @ManyToMany relationship is stored in table A_B, A's entry in the DB would not alter, the version field would not get incremented, so summarized that approach does not work for me.

Locking entities in A_B would not work either as in a @ManyToMany new rows could be inserted that affect my two classes. I'd need some kind of range-based lock to achieve that no links between my instances of A and B are altered, added or deleted. Unfortunately, I did not find a proper solution for that problem.

What would be the best way to lock entities of A including all relationships to other entities?

4

0 回答 0