-->
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.  [ 4 posts ] 
Author Message
 Post subject: Database triggered versioning
PostPosted: Fri Feb 24, 2006 8:56 pm 
Newbie

Joined: Fri Feb 24, 2006 8:26 pm
Posts: 5
I'm using Hibernate Annotations 3.1beta8 and I'm trying to implement optimistic locking on an existing table. To enforce version numbering the version number field is automatically updated by a database trigger, and that trigger throws an error if the application tries to update the version number.

If I mark the field as versioned
Code:
'@Version private int record_version_number;'
then hibernate tries to set the field on every update causing the update to fail. If I set the field as not updatable
Code:
@Version @Column(updatable=false) private int record_version_number;
then hibernate doesn't do optimistic locking. Adding '@GeneratedValue' to it doesn't change anything.

Is there anyway to tell hibernate to do optimistic locking but not try to update the version number itself? Also more generally is there anyway to tell Hibernate the column is updated by the database on every update, not just on insert?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 12:03 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'm not sure if this is doable, maybe by implementing a usertype (VersionType)

There is a generated=true on hbm files but there is no equivalent in annotations so far

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Hacky solution
PostPosted: Thu Mar 02, 2006 9:32 pm 
Newbie

Joined: Fri Feb 24, 2006 8:26 pm
Posts: 5
I've managed to find a work around that didn't require using the xml config files but instead used a new annotation to indicate what properties should be loaded dynamically.

First I add all the classes to the AnnotationConfiguration as usual. After that I call buildMappings to ensure all the mappings are created and all the hibernate annotations applied. Then I go back over all entity classes looking for my new annotation. If I find it I use getClassMapping and then getProperty to get the property for the annotated element and then use the setGeneration method on Property to set the generation time to PropertyGeneration.ALWAYS. After I've done that hibernate handles the optimistic locking the way I need it to.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 10:06 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That a possibility but it would be much easier, faster at runtime to patch AnnotationBinder itself

_________________
Emmanuel


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