-->
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: save() doesn't return id
PostPosted: Thu Dec 14, 2006 12:02 pm 
Newbie

Joined: Thu Dec 14, 2006 9:42 am
Posts: 2
For some reason I don't get the id (primary key) of the object saved with session.save(object).... I have the following:

Code:
<id name="udetId" type="int">
  <column name="udet_id" />
  <generator class="assigned" />
</id>

and:

Code:
Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
sess.beginTransaction();

UserDetail ud = new UserDetail();
ud.setUdetName("Asdf");
ud.setUdetSurname("Asdfasdf");
ud.setUdetMail("asdf@asdf.com");

java.io.Serializable udId = sess.save(ud);
sess.getTransaction().commit();


When I later print the value of udId, it comes out to be 0.
Any ideas?


Top
 Profile  
 
 Post subject: generator class='assigned'
PostPosted: Thu Dec 14, 2006 12:07 pm 
Newbie

Joined: Wed Dec 06, 2006 6:30 pm
Posts: 8
You have specified that you will assign the id (see <generator .../> mapping). You need to set the id before you save it.

In fact, if you do not specify a generator then the default is 'assigned'. Therefore your mapping could be simplified to:

Code:
<id name="udetId" type="int" column="udet_id" />


-David


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 4:48 am 
Newbie

Joined: Thu Dec 14, 2006 9:42 am
Posts: 2
Arrgh!
Thanks a lot! The problem was so annoying.. I thought that using this generator and save() function the workflow looks like this:

1. Hibernate generates the object,
2. asks db for the id to use,
3. saves the object into db,
4. returns the used id

So I thought that the error lies in my usage of the object, not in mapping..
Anyway, I used
Code:
<generator class="increment" />
and now everything works fine.

Thanx again!


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.