-->
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: Transient->Persistent via Session.save(): id not availabl
PostPosted: Thu Mar 09, 2006 10:43 am 
Newbie

Joined: Wed Mar 01, 2006 3:23 pm
Posts: 8
Hibernate version: 3.1.2

Mapping documents:
Code:
<class name="reger.dao.hibernate.Image" table="image">
        <cache usage="nonstrict-read-write"/>
        <id name="imageid" type="int">
            <column name="imageid" />
            <generator class="assigned" />
        </id>
     
        ...

</class>



Code between sessionFactory.openSession() and session.close():

Code:
//Create a transient Image object
Image newimage = new Image();
newimage.setFilename("myfile.jpg");
newimage.setEventid(eventid);
Session hsession = HibernateUtil.getSession();
hsession.beginTransaction();
//Convert it to a persistent object
hsession.save(newimage);
hsession.getTransaction().commit();
//Problem here: imageid is reported as 0 after save.
System.out.println("imageid=" + newimage.getImageid());
hsession.close();


Name and version of the database you are using:
MySql 5.0

The generated SQL (show_sql=true):
Hibernate: insert into image (eventid, accountuserid, image, description, sizeinbytes, imageorder, originalfilename, accountid, filename, imageid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Problem description:
I create a transient instance of Image called newimage. At this point the identifier (imageid) is, of course, 0. I expect this.

I then call save() on the instance, converting it from transient to persistent. The database is correctly updated and an imageid>0 is generated by MySql auto increment. The row appears properly in the database.

However, when I then call newimage.getImageid() I get 0. I would expect to get the new imageid that the database has assigned.

Questions:
1) Am I wrong to expect that the persistent object will reflect the identifier value after being converted from a transient with the save() method?

2) How can I get the identifier of a newly-persisted object? I've tried calling hsession.refresh(newimage) after save() but am told that a row with imageid=0 does not exist in the database. Makes sense as the imageid is not populated on save(), but I'm hoping there's a way to get the new id.

3) I'm caching the Image class... is there anything special I need to do to get the identifier after a conversion from transient to persistent when the class is cached?

Thanks all!

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 11:05 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Hi,

you are using mysql right?
generator class should be:
Code:
<generator class="increment"/>


Quote:
increment:
generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.


(At Hibernate startup, this generator reads the maximum primary key column value
of the table and increments the value by one each time a new row is inserted. The
generated identifier is of type long, short, or int. This generator is especially
efficient if the single-server Hibernate application has exclusive access to the
database but shouldn’t be used in any other scenario.)

Quote:
assigned:
lets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.


This is a quote from the book Hibernate in action:

Quote:
The special assigned identifier generator strategy is most useful for entities with
natural primary keys. This strategy lets the application assign identifier values by setting the identifier property before making the object persistent by calling
save(). This strategy has some serious disadvantages when you’re working with
detached objects and transitive persistence (both of these concepts are discussed
in the next chapter). Don’t use assigned identifiers if you can avoid them; it’s
much easier to use a surrogate primary key generated by one of the strategies listed
in table 3.1


yours simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 11:39 am 
Newbie

Joined: Wed Mar 01, 2006 3:23 pm
Posts: 8
Ah, perfect... thanks for the help Simon. I'm clustered so I couldn't use increment but your help pointed me to the source of the problem... the identifier generation mechanism. I was playing around with Session for hours :) I ended up using the "native" generator. Thanks!


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.