-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problems with SaveUpdate Cascade
PostPosted: Thu Oct 04, 2007 9:44 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 7:41 am
Posts: 24
Hi,

I've got kind of a dilemma here with the SAVE_UPDATE cascade style in annotations. I do have an abstractmedia object that has a ManyToOne relation to a creator object and it seems that using SAVE_UPDATE is the only way of letting Hibernate persist new creator objects when updating the abstractmedia object. So the abstractmedia object is only updated, but before that the creator object needs to be persisted. This only happens when I specify SAVE_UPDATE (I'm using the saveOrUpdate method for the abstractmedia object), when I leave it out I get an except that I need to persist the creator first.

Now the dilemma is that when I change the creator to another one and use saveOrUpdate that new creator object is already associated to the session and thus the update on the creator fails with a NonUniqueObjectException (because saveOrUpdate cascade's to the cached creator).

I currently can't see a way to find out wether a given object is associated to a session and thus there's no way for me to determine wether the creator object attached to an abstractmedia object needs to be persisted or not. The code that loads existing creator's from the database to attach them to the abstractmedia object is completely detached from the code that saves or updates an abstractmedia object.

Anybody understood that and has an idea how to fix that (without having to do something like if creator.id == 0 -> persist)?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 10:18 am 
Beginner
Beginner

Joined: Thu Aug 31, 2006 7:41 am
Posts: 24
I'd like to add that removing SAVE_UPDATE and using something like

if( session.contains( m.getCreator() )
{
session.persist(m.getCreator());
}
...

Doesn't work because the instance returned by m.getCreator() is not in the session but another instance with the same identifier.

Hmm, seems like the nonunique object exception only occurs when I change the creator to one that is used also by another abstractmedia object. I guess the other abstractmedia object is loaded due to the relationships and with it its creator instance. What I don't understand is why Hibernate doesn't return this already-existing creator instance when I execute a query that looks for all creator's in the database. Instead hibernate seems to load the creator instance for a second time.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.