Getting OptimisticLockException when dealing with Link Tables

2 messages Options
Embed this post
Permalink
srini.krish

Getting OptimisticLockException when dealing with Link Tables

Reply Threaded More More options
Print post
Permalink
Hi,
 I face a weird problem with the following relationship.
I have an Organisation, which can have Users and Groups. Users and Groups are associate to each other.

I have a UserGroup entity, to represent the link table, as I prefer not to have ManyToMany relation b/w user and group.

Now, If I delete User, the UserGroup association need to be deleted.
The same should happen if I delete Group also. So far everything is good.

But now, when I delete Organisation, cascade is happening on both User and Group. This in turn, tries to delete the same UserGroup association twice. This is causing OptimisticLockException.

I tried Cascade, ForeignKey, Dependent in all possible ways :-). But nothing is working.

I have 3 questions.

1. Is there any way to achieve the above functionality?

2. Why OpenJPA throws OptimisticLockException, if I try to delete the same row twice inside a single transaction?

3. Can OpenJPA resolve this kind of two way relations smartly? I mean only one delete should be fired.

Thanks,
Srinivasan Krishamoorthy.
Pinaki Poddar

Re: Getting OptimisticLockException when dealing with Link Tables

Reply Threaded More More options
Print post
Permalink
> 3. Can OpenJPA resolve this kind of two way relations smartly? I mean only one delete should be fired.

OpenJPA uses a fairly smart topological sort to order its SQL operations. The algorithm is sufficeintly smart to detect many other cases where ordering of generated SQL is critical in terms of satisfying existing database constraints. Your observation suggests that the presented use case where the same instance becomes a candidate for the delete set twice (or more) is not handled.
  Please present your use case in its essential form with a reproducible test case and we will follow it up.

  Regards --




srini.krish wrote:
Hi,
 I face a weird problem with the following relationship.
I have an Organisation, which can have Users and Groups. Users and Groups are associate to each other.

I have a UserGroup entity, to represent the link table, as I prefer not to have ManyToMany relation b/w user and group.

Now, If I delete User, the UserGroup association need to be deleted.
The same should happen if I delete Group also. So far everything is good.

But now, when I delete Organisation, cascade is happening on both User and Group. This in turn, tries to delete the same UserGroup association twice. This is causing OptimisticLockException.

I tried Cascade, ForeignKey, Dependent in all possible ways :-). But nothing is working.

I have 3 questions.

1. Is there any way to achieve the above functionality?

2. Why OpenJPA throws OptimisticLockException, if I try to delete the same row twice inside a single transaction?

3. Can OpenJPA resolve this kind of two way relations smartly? I mean only one delete should be fired.

Thanks,
Srinivasan Krishamoorthy.
Pinaki