-->
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: Disable optimistic locking
PostPosted: Thu Sep 06, 2007 6:52 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
Hibernate version:
3.1.0
Mapping documents:
...
<version column="count_upd" name="countUpd" type="long"/>
...
Code between sessionFactory.openSession() and session.close():
SessionFactory sf = config.buildSessionFactory();
Session hibSession = sf.openSession( con );

tstDACmain main1 = new tstDACmain();
main1.setDes("Test merge 1");
hibSession.save(main1);
hibSession.flush();

main1.setDes("Test merge 2");
main1.setCountUpd(new Long(1));
hibSession.update(main1);
hibSession.flush();


Mine is just a question: I need in a particular circumstance (the bolded line in the code above) to disable optiomist locking and force my "version" column to a value.
Of course only in that particular case, not always, so I cannot change the mapping.
Is this possible?
Thanks for help
Alessandro


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 06, 2007 8:14 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
Why do you want to do that? Whatever the reason is: search for another sollution!

I don't think that that what you want to do is possible. In the book "Java persistence with hibernate" the author describes how hibernate handles version's. Hibernate fires an update statement like this.

Code:
update MyTableName
set version = <oldversion + 1>
....
where id = ....
and version = <oldVersion>


If hibernate fires this statement and the jdbc driver retruns that no entity has been updated, hibernate knows that another bean has already updated this entity and fires an OptimisticLockingException. Otherwise the update was ok.

I think the current version attributes value (in your case: ="countUpd") is set as a parameter in the query, so if you do what you described above, you will get an exception - because you changed the version attributes value.

Also have in mind, that if your read an entity, the version attribute could be incremented too! So you can not use a version field for an "real" update counter (because of the version attributes name I think that you want to do that).

I often declare my version's attribute's getter and setter private. I think everybody should do that.

Regards
Hoeft


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 06, 2007 9:35 am 
Regular
Regular

Joined: Thu Sep 04, 2003 10:43 am
Posts: 61
hoeft wrote:
Why do you want to do that? Whatever the reason is: search for another sollution!

I don't think that that what you want to do is possible. In the book "Java persistence with hibernate" the author describes how hibernate handles version's. Hibernate fires an update statement like this.

Code:
update MyTableName
set version = <oldversion + 1>
....
where id = ....
and version = <oldVersion>


If hibernate fires this statement and the jdbc driver retruns that no entity has been updated, hibernate knows that another bean has already updated this entity and fires an OptimisticLockingException. Otherwise the update was ok.

I think the current version attributes value (in your case: ="countUpd") is set as a parameter in the query, so if you do what you described above, you will get an exception - because you changed the version attributes value.

Also have in mind, that if your read an entity, the version attribute could be incremented too! So you can not use a version field for an "real" update counter (because of the version attributes name I think that you want to do that).

I often declare my version's attribute's getter and setter private. I think everybody should do that.

Regards
Hoeft


I have to do that within a lib function ... don't worry why, I need it ... I do not have lock problem in that circumstance, for the operations that I've already done before.
If this is not possible I have to use a direct JDBC call ... so 2 query instead of one ...
Btw thank you for your support


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.