This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refresh a relationship

 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Do I have to make a query?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic