-->
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.  [ 8 posts ] 
Author Message
 Post subject: Using @GeneratedValue for non-key persistent field
PostPosted: Thu Apr 10, 2008 3:44 pm 
Newbie

Joined: Thu Mar 27, 2008 10:50 am
Posts: 13
Hibernate version: 3.2.4.sp1
Mapping documents: Using annotations
Name and version of the database you are using: Microsoft SQL Server 2005

I am using Hibernate through JPA and would like to have values for a certain non-key persistent field be automatically generated based on an @GeneratedValue and @TableGenerator. However, it does not work. The value in the concerned column in the database is always 0 (zero) in every row.

Is it really possible to generate values for non-key column using @GeneratedValue? Is there something special to be done to get it working?

A search in the forums gave me http://forum.hibernate.org/viewtopic.php?t=968325, which is interesting but not really specifically about @GeneratedValue.

FWIW, http://www.manning-sandbox.com/thread.jspa?threadID=23932 is another place where I am trying to get some closure on this

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Using @GeneratedValue for non-key persistent field
PostPosted: Thu Apr 10, 2008 4:06 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I thought @GeneratedValue on a non-primary key means hibernate will do a select after insert to get the field value from database, and it does not mean hibernate will generate a value for it. You should be able to verify this if you see a select the_column from the_table statement after inserts.



Farzad-


Top
 Profile  
 
 Post subject: Re: Using @GeneratedValue for non-key persistent field
PostPosted: Thu Apr 10, 2008 4:10 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I messed up. It's @Generated not @GeneratedValue and in Hibernate documentation @GeneratedValue is always mixed with @Id so it might imply that's the only place it works.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 4:26 pm 
Newbie

Joined: Thu Mar 27, 2008 10:50 am
Posts: 13
The first line of javadoc for the currently released spec at http://java.sun.com/javaee/5/docs/api/javax/persistence/GeneratedValue.html seems to indicate that @GeneratedValue is specified to ACTUALLY WORK only for key fields.

Oh well, here's hoping that this will be specified to work even for non-key fields in JPA 2.0


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 4:28 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
neville.sequeira wrote:
Oh well, here's hoping that this will be specified to work even for non-key fields in JPA 2.0



Still you can use @Generated and have the database handle this for you.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 5:04 pm 
Newbie

Joined: Thu Mar 27, 2008 10:50 am
Posts: 13
Farzad,

Using the Hibernate @Generated means telling Hibernate that the databse will generated the value of the concerned field. I have not tried this, but would imagine such a scheme to be non-portable accross databases. It would be great if you could elaborate on what you are eluding to, even if it implies a non-portable scheme of thins.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 5:11 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
@Generated tells hibernate that this field gets a value in database and hibernate should fetch its value. Then you could put the generation logic in database. It is pretty much portable since it is only a select; however, I am not sure if all databases have a solution for default values and/or triggers.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 5:30 pm 
Newbie

Joined: Thu Mar 27, 2008 10:50 am
Posts: 13
Unfortunately, we do not have the freedom of using trigger or it's friends to generate such values. The reason is portability concern. We are developing software of a platform nature and would not want the slightest of portability risks to creep in. And, it's not really a default value we are looking for but rather a value that is unique across the rows. I think, currently, my only option is to implement a Java based identifier value generating scheme(maybe based on java.util.UUID), which is a shame given the presence of JPA and providers such as Hibernate - unless there is way to leverage specific generators in Hibernate such as org.hibernate.id.UUIDHexGenerator in a non-key field context.

I am on the wrong path or a non-ideal path.


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