-->
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.  [ 2 posts ] 
Author Message
 Post subject: Lock/refresh nighmare, please help
PostPosted: Tue Oct 23, 2007 8:38 am 
Newbie

Joined: Fri Mar 24, 2006 1:20 pm
Posts: 7
Hello everyone,

I have a very simple problem, but I have not found an easy solution. This indicates that i must be doing wrong :

I want to lock an object with a LockMode.UPGRADE lock, then read the object attributes, as they are in the DB. This should translate into one query : select * from .. where id=.. for update.

I tried :

Session.lock(object,LockMode.UPGRADE). This translates into select ID from .. where id=.. for update. So the lock is created, but the state of the locked object is not re-read from the db. Not very usefull.

Session.refresh(object,LockMode.UPGRADE). This doesnt even make a query when object is a unitialized proxy, so no lock is made. This sounds like a bug to me. This does however make the correct query when object is an initialized proxy.

The only solution I found, that works in any cases, was to make the select myself using an HQL query and use the Query.setLockMode(object,LockMode.UPGRADE) method.

Can someone help me please?

Thanx,


Antoine


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 02, 2007 10:09 pm 
Newbie

Joined: Thu Mar 23, 2006 10:52 am
Posts: 4
I've run into the same problem with refreshing an un-initialized proxy, and I also think it's a bug. It's even been reported, and not yet rejected (but not assigned, either): http://opensource.atlassian.com/project ... e/HHH-1645 .

I didn't find that ticket the first few times I searched for it, but there it is!

This approach should work:

Session.lock(object, LockMode.UPGRADE);
Session.refresh(object);

If you'd rather use just one query -- which most people would -- I don't know of a way to do it without HQL/SQL/Criteria. But then you end up writing something specific to each type of object, as you've probably noticed.

If you're using Java 1.5 or later, and all of your entity classes implement an interface that has a getId() method for a corresponding "id" property (or whatever names you prefer), then you could generalize the Criteria solution. It would be nicer if the API worked as expected, though. :)


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