• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: correct handling of all-delete-orphan

 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just wondering how to properly edit object:

Class A

@OneToMany
@Cascade (All, Delete_Orphan)
Set<B> bObjects;

//some other fields of A below

Since cascade type delete-orphan requires that the Collection Set<B> bObjects be loaded by Hibernate(and not detached) in order to delete the bObject orphans, what I do is load the A object from database then edit the b elements (through clear then addAll)

public void updateA(A aObject) {

//I need to persist the aObject that I receive here

}

It could be that other fields of A has changed, or the b elements individually has changed, the Set of bObjects has increased or decreased in no.

Could somebody please help in implementing this? My solution right now is to manually copy the fields from aObject to the aObject I got from DB but this is not a very good solution
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Since cascade type delete-orphan requires that the Collection Set<B> bObjects be loaded by Hibernate(and not detached) in order to delete the bObject orphans"

What exactly do you mean by that? Do you mean that in order to remove an object from a Set that that object has to be in the Set? Like a real object and not a proxy. Although I think proxy should be fine too.

Anyway. If you have to pre-load the Set, you can do that with a fetch join in your query.

Are you seeing any errors, if you don't do it the way you are doing? Maybe you can post your error and we can find you a better solution.

Mark
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Here is the exact code(Class names modified)

public void doUpdateA(A aObject) {

//aManager.read loads the object
A persistentA = aManager.read(aObject.getId());

//AUtils.copy copies field values from aObject to persistentA
AUtils.copy(aObject, persistentA);

//I clear the set because I need the original collection instance loaded by hibernate
persistentA .getBset().clear();

//to avoid not unique exception, I set bobject id to null
for(B bObject: aObject.getBset) {
bObject.setId(null);
}

persistentA.getBset().addAll(aObject.getBset());

}

the scenario is that an aObject is loaded by hibernate, then detached and sent to front-end, edited, some bObjects edited, added or removed, then finally received in this method. I am expected to make the database entry reflect this modified aObject and delete all orphaned bObjects. Flushing is handled by Spring by the way

If still unclear, I would gladly provide more clarifications
[ December 20, 2007: Message edited by: Ronwaldo Cruz ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't that it is unclear. It is that if you don't have the association mapped, then you are left with having to code it yourself.

Mark
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm sorry I don't understand...

Isn't this the mapping that I need?

Class A

@OneToMany
@Cascade (All, Delete_Orphan)
Set<B> bObjects;
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that basically looks like it. As long as you have static imports so that you don't get a compile error.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So then I stand by my first reply. If your query eager fetches the collection, then you won't have to "edit" all the collection members (to load the data) in order to delete them.

Mark
 
Rashid Darvesh
Ranch Hand
Posts: 189
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rashid Darvesh:
Have a look at this
http://www.makemyinfo.com/Tutorial.do?action=getTutorial&tutId=T0005.jsp


Rashid, please refrain from blatantly advertising your tutorial. You posted it in a few threads that already were complete. We do not condone this type of behaviour.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rashid, your account has been suspended, because longing back in history, you have been warned before, and we do not tolerate blatant advertising. If you really want to help people out and actually answer their questions rather than just posting a link then let me know.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic