-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Unidirectional many-to-many owning entity removal
PostPosted: Fri Jan 04, 2008 3:23 am 
Newbie

Joined: Fri Jan 04, 2008 3:10 am
Posts: 4
Hello,

Currently we are struggling to port our system to pure JPA (till now we were using JDO). We've encountered problems with unidirectional many-to-many association (class A has a collection of elements of class B, class B has no reference to class A). The problem is, that upon attempt of removal of an object of class A (the owning side), the operation fails with following exception:

Code:
org.postgresql.util.PSQLException: ERROR: update or delete on "A" violates foreign key constraint "fk2e5f270992a3d5a2" on "A_B"
  Details: Key (identifier)=(64) is still referenced from table "A_B".


As A is the owning side of the association, why is it not possible to remove the object of this class, automatically discarding the association? Is it possible to achieve this effect with another combination of annotations?

I've tried to make an one-to-many association with overriding inverse join column unique property, but the trick does not work (the foreign key in the join table is created with an unique constraint regardless of the @JoinTable.@JoinColumn settings).

Help greatly appreciated, as I'm trying to deal with the problem for 3 days now...

best regards
Marek


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 3:40 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Amazarak,

Did you specify the "cascade" atribute on the A side of the mapping?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 3:47 am 
Newbie

Joined: Fri Jan 04, 2008 3:10 am
Posts: 4
gonzao_diaz wrote:
Did you specify the "cascade" atribute on the A side of the mapping?


I've tried different ways (all, none, persist+remove, etc.) and it does not change the situation. As I understand relationship cascading, it concerns entities, not the association itself. That is, with cascade delete, removal of an object of class A, would remove referenced objects of class B (which is questionable in a many-to-many relationship, as other objects of class A may reference the same objects of class B).

[edit]
jsr220 reads (p. 24, bottom):
Quote:
The relationship modeling annotation constrains the use of the cascade=REMOVE specification. The cascade=REMOVE specification should only be applied to associations that are specified as One-
ToOne or OneToMany. Applications that apply cascade=REMOVE to other associations are not portable.
[/quote]


Top
 Profile  
 
 Post subject: Minimal test case
PostPosted: Fri Jan 04, 2008 4:44 am 
Newbie

Joined: Fri Jan 04, 2008 3:10 am
Posts: 4
I've created a minimal test case.

Removal works properly if I use select, em.remove() sequence.

When I use batch removal of objects of class A (via Query.executeUpdate()), the operation fails with FK constraint. May this be the bug in query generation code?

[edit]

jsr220 states (page 104):
Quote:
A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to
related entities.


Nothing is mentioned about the relationships, though. Creating the join table with foreign key to owning entity as DELETE CASCADE should solve the problem.

For now the only solution to perform batch remove with such relationship is to select all entities and them iterative em.remove(). Well, this may be slow.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 11:53 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Dude, write cascade="all-delete-orphan" on your set and stop complicating yourself.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 12:03 pm 
Newbie

Joined: Fri Jan 04, 2008 3:10 am
Posts: 4
gonzao_diaz wrote:
Dude, write cascade="all-delete-orphan" on your set and stop complicating yourself.


Unfortunately, we have to be 100% compatible with JPA... Not yet decided if Hibernate is going to be our ultimate provider.

I've changed batch deletes to be done by EntityManager's remove() method, and now everything works fine. This is not an application that would require extreme robustness, so I hope this will do.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.