Hibernate: lockMode for criteria is not working

2.8k views Asked by At

I need to specify lock mode for hibernate. What I am doing:

session().createCriteria(clazz, "c")
  .add(Restrictions.eq("c.a", false))
  .add(Subqueries.propertyEq("c.b", subquery))
  .setLockMode("pos", LockMode.PESSIMISTIC_READ);

But when I see provided query - hibernate still doesn't provide SELECT FOR UPDATE

How can I force hibernate to make SELECT FOR UPDATE clause? I see only case when it works is this:

session().get(clazz, id, LockOptions.UPGRADE);

But I need to use more complex query.

1

There are 1 answers

0
Igor Konoplyanko On BEST ANSWER

I've found the reason why it's wasn't working. It's actually bug in hibernate 3.5-3.6 And it's fixed only in 4.0.1.

https://hibernate.atlassian.net/browse/HHH-5275

So, I've ended with this workaround:

MyObject myObject = criteria.uniqueResult();
MyObject lockedOne = (MyObject) session()
                       .get(MyObject.class, myObject.getId(), LockMode.UPGRADE_NOWAT);