aspose file tools
The moose likes Object Relational Mapping and the fly likes Refresh a relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Refresh a relationship" Watch "Refresh a relationship" New topic
Author

Refresh a relationship

Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
Hi there,
I've got these three entities in a sample project based on an ipotetic bus time table, in order to make some practice with hibernate and persistence in general.

Two of these entities are related to one called ServiceStop, which basically is a single stop for every bus service.

I'm havig some troubles with the remove action (I haven't tried the update yet), I can successfully remove the service and the serviceStop related, but the stop entity keeps the relation.
What's the best approach to remove the relation only and keep the stop entity?

JBoss 4.0.5
Hibernate

My classes
Service


ServiceStop

Stop


EJB Bean Method


Thanks in advance


trying to decode a woman mind....
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

It is all based on the setting of the cascade relationship. Now Hibernate has a cascade option called delete-orphan that JPA does not have. You can try setting the relationship cascade to add the hibernate cascade option of delete-orphan.

So how are you calling remove, what object are you passing in to the method, or is it that you are removing the child objects from the list?

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
I just want to remove the child (ServiceStop) from the List in Stop.
I've tried with this

first attempt

this way also removes the Stop entity, while I want to keep it.

while adding the annotation you suggested on Stop class, still keeps me the the relation with ServiceStop id

2 attempt


this is what my application test calls on the ejb interface

Only the service object is passed.
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218


Do I have to make a query?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16624

OK, I think I am starting to see what you are trying to get at.

You don't want any records deleted, you just want the FK to be nulled out right?

So when it comes to mapping relationships, it is important sometimes to make it bi-directional, as in your object code you are doing. So if you have a mapping in one direction and a mapping in the other direction, ORM does not know that this maps to the same relationship. You need to have one side declared as the inverse.

So on one side, you will included a "mappedBy=" annotation attribute. And point to the other classes instance var that has the same relationship mapping.

You probably can remove the delete-orphan because that definitely deletes the record if you remove it from the list.

Mark
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
It works.

Before use the element mappedBy it had (now disappeared) a join table with StopID and ServiceStopID. Did it use that beacause it didn't know what was the owner??

Thank you very much for your support.
[ August 17, 2007: Message edited by: Alessandro Ilardo ]
 
 
subject: Refresh a relationship
 
Threads others viewed
Why is my Hibernate Search query not working?
could not initialize proxy - the owning Session was closed
deleteing rows
IllegalArgumentException: cannot remove/save a detached entity
using 2 @OnetoMany annoations in one Entity Class
MyEclipse, The Clear Choice