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.  [ 3 posts ] 
Author Message
 Post subject: Another beginners question
PostPosted: Mon Sep 21, 2009 12:04 pm 
Newbie

Joined: Mon Sep 21, 2009 11:57 am
Posts: 2
Hi there,

when an Object is saved, using saveOrUpdate, how can I find it back if only the new ID is unique? Isnt there any function that automatically returns the new ID instead of 'void'?

thanks,
Klaas


Top
 Profile  
 
 Post subject: Re: Another beginners question
PostPosted: Mon Sep 21, 2009 5:38 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi poobeer,

any new identifier is directly assigned to the object you saveOrUpdate - so if you access the object's getId() (or whatever your method name is in this case) right after calling saveOrUpdate() for the object you get its ID.

If that is not suitable you could implement your own version of saveOrUpdate:
Code:
if (obj.getId() == null) {
  save(obj);
} else {
   update(obj)
}

This will not work if you use assignment generator on your identifier.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Another beginners question
PostPosted: Tue Sep 22, 2009 11:55 am 
Newbie

Joined: Fri Jan 23, 2009 8:01 pm
Posts: 13
I use merge() and then the new object's ID is updated in the reference when it's created. So you can just return the TV (or the id) after it's merged.

Code:
IE:
   public TV storeTV(TV tv) {
      sessionFactory.getCurrentSession().merge(tv);
      return tv;
   }


Keep in mind if you are using spring like me, I saw this note in the petclinic example:

// Note: Hibernate3's merge operation does not reassociate the object
// with the current Hibernate Session. Instead, it will always copy the
// state over to a registered representation of the entity. In case of a
// new entity, it will register a copy as well, but will not update the
// id of the passed-in object. To still update the ids of the original
// objects too, we need to register Spring's
// IdTransferringMergeEventListener on our SessionFactory.

Hope that helps.


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