-->
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.  [ 5 posts ] 
Author Message
 Post subject: Create and return: the simplest way
PostPosted: Sun Feb 22, 2009 8:23 am 
Newbie

Joined: Wed Dec 24, 2008 1:29 pm
Posts: 7
In my code I need to create and return created object, because id is assigned by a data base.

I use such code:

protected T saveAndReturn(final T obj) {
__logger.assertNotNull(obj, "Do not save null objects");
HibernateTemplate hibernateTemplate = getHibernateTemplate();

return (T) hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException {
Long id = (Long) s.save(obj);
return s.load(obj.getClass(), id);
}
});
}

i.e. I call load in order to get the object with the assigned id.

Is there in hibernate a simplest way?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 2:34 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
There is no need for loading the obj again after saving. Once you call the save, obj is persisted and the obj will reflect the DB row, the id field and all will get initialized by itself. So after save you can just return the obj.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 3:58 am 
Newbie

Joined: Wed Dec 24, 2008 1:29 pm
Posts: 7
Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:01 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Plz rate any posts which helped you. Coz we need credits to ask doubts and it doesn't cost you anythng to rate the posts.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:11 am 
Newbie

Joined: Wed Dec 24, 2008 1:29 pm
Posts: 7
Thank you!


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