Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » ManyToMany: Why is no exception thrown?
ManyToMany: Why is no exception thrown? [message #379033] Mon, 11 August 2008 09:57 Go to next message
Christian Reiter is currently offline Christian ReiterFriend
Messages: 4
Registered: July 2009
Junior Member
Hi!

I've defined two Entity Objects named Person and City. They have a
ManyToMany JPA relationship.

The relationship is established through a relation table in the DB named
CITY_PERSON.

My Problem is that when i try to remove a City which is "in
use"/referenced by some Person i do not get an exception, instead the City
as well as the row in CITY_PERSON gets deleted without a warning.

When i try to remove a Person i do get a RollbackException as expected and
the Person is not removed.

Why do i not get an exception when removing a used/referenced City?

Is there any way to check if any entity has references prior to removing
it.

Best regards,
christian
Re: ManyToMany: Why is no exception thrown? [Code Example] [message #379034 is a reply to message #379033] Mon, 11 August 2008 09:58 Go to previous messageGo to next message
Christian Reiter is currently offline Christian ReiterFriend
Messages: 4
Registered: July 2009
Junior Member
PS:
You can review my code here:
http://web.percom.at/ManyToMany.html
Re: ManyToMany: Why is no exception thrown? [message #379036 is a reply to message #379033] Mon, 11 August 2008 13:21 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
JPA does not maintain bi-directional relationships for you. When you
remove an object or one side of a relationship you are responsible for
maintaining the other side of the relationship.

You do not get the error from one side because it owns the relationship
and removes the entries from the join table when being deleted, the other
side is read-only so does not, and gets a database constraint error. In
both cases your application or model code is responsible for removing all
references to the object being deleted.

You could potentially use a preRemove event to traverse your object's
relationships and remove its' references. Although if the user is
deleting an object that has references you may consider this application
logic.

-- James
Re: ManyToMany: Why is no exception thrown? [message #379037 is a reply to message #379036] Mon, 11 August 2008 13:39 Go to previous messageGo to next message
Christian Reiter is currently offline Christian ReiterFriend
Messages: 4
Registered: July 2009
Junior Member
Thanks James for your fast response!

Is there any JPA- or EclipseLink-specific way to check if an Entity has
any refrences? Or do i have to implement this myself?

best regards,
christian
Re: ManyToMany: Why is no exception thrown? [message #379038 is a reply to message #379037] Mon, 11 August 2008 14:33 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 817
Registered: July 2009
Senior Member
> Is there any JPA- or EclipseLink-specific way to check if an Entity has
> any refrences? Or do i have to implement this myself?

I prefer reverse engineering the BM from the database: you then know for sure that all relations have the appropriate handling.
Re: ManyToMany: Why is no exception thrown? [message #379263 is a reply to message #379038] Tue, 12 August 2008 06:44 Go to previous messageGo to next message
Christian Reiter is currently offline Christian ReiterFriend
Messages: 4
Registered: July 2009
Junior Member
Hi tbee!

What means "BM"?

thanks,
christian
Re: ManyToMany: Why is no exception thrown? [message #379265 is a reply to message #379037] Tue, 12 August 2008 13:26 Go to previous message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
There is no specific way. If you make the relationships bi-directional,
then you can at least navigate to the references from the object.
Otherwise you would need to perform a query to find the references.

-- James
Previous Topic:OSGI troubles with createEntityManagerFactory
Next Topic:ManyToOne in Embeddable
Goto Forum:
  


Current Time: Tue Apr 16 16:54:17 GMT 2024

Powered by FUDForum. Page generated in 0.51315 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top