-->
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.  [ 10 posts ] 
Author Message
 Post subject: Object saveOrUpdate(Object)
PostPosted: Mon Jul 25, 2005 8:21 am 
Beginner
Beginner

Joined: Mon Jun 20, 2005 3:14 pm
Posts: 33
Hi,

I would like to return the Object saved into database with appropriate fields values (because PK is auto-generated). Is there a way to do this using saveOrUpdate?

Suppose this situation:

Code:
class MyObject {
  long id = 0; // <--- before save
  String desc;
}

myObject = new MyObject();
myObject.setDesc("first object created");
MyObject updatedObj = session.saveOrUpdate(myObject);

System.out.println(updatedObj .getId()); // <--- must print '1' (auto-generated key)


Thanks,

Andre


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 8:34 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
If you don't need the saveOrUpdate, you can use:
Code:
Session session = ...;
Serializable id = session.save(Object object);


Otherwise try using this:
Code:
Session session = ...;
Serializable id = session.getIdentifier(Object object);


In your case:
Code:
Serializable id = session.getIdentifier(myObject);


See JavaDoc for more information.

Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 8:45 am 
Beginner
Beginner

Joined: Mon Jun 20, 2005 3:14 pm
Posts: 33
Sven,

Thanks for your reply, but I would like to return the whole object, included new ID (not only ID object)

Thanks,

André


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 8:58 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Code:
...
Serializable id = session.getIdentifier(myObject); // resp. Seriablizable id = session.save(myObject);
MyObject myObject = (MyObject) session.load(MyObject.class, id)


Alternatively, use Criteria or HQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 9:04 am 
Beginner
Beginner

Joined: Mon Jun 20, 2005 3:14 pm
Posts: 33
Sven,

But if my object has a composite PK?

Thanks again,

Andre


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 9:05 am 
Beginner
Beginner

Joined: Mon Jun 20, 2005 3:14 pm
Posts: 33
Sven,

But if my object has a composite PK? what kind of data will getIdentifier()return?

Thanks again,

Andre


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 10:55 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Well, I don't know what getIdentifier() will return in case of a composite PK.

Use Criteria or HQL to retrieve your object with id.

Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 6:08 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
1. saveOrUpdate will modify the object you pass it to set the id.

2. Composite keys are always set by you (using generator class="assigned"), and will therefore always be set before you even call save, if your code is to work.


Top
 Profile  
 
 Post subject: refresh
PostPosted: Mon Jul 25, 2005 11:56 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If saving the object modified fields other than the id (e.g. if the DB fills in a version or timestamp field) then session.refresh(object) could be what you're looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 26, 2005 6:47 am 
Beginner
Beginner

Joined: Mon Jun 20, 2005 3:14 pm
Posts: 33
Thanks guys!


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