0

I want to perform select for update operation using hibernate 4 with simple POJO. Can somebody share me step by step example how I can achive this task. I google it but there are not step by step examples all are small code snippets where I understand that select for update using hibernate can only be performed with pessimistic locking technique.

Thanks,

DKamran

D Kamran
  • 43
  • 9
  • Why is http://stackoverflow.com/questions/6872083/spring-pessimistic-locking , just to give an example, not sufficient for you? Could you try to explain what you don't understand? – John Donn Oct 18 '16 at 10:46

1 Answers1

0

Two ways to do this are:

getMyClassObjects(session).stream().
   forEach(
             m -> session.load(MyClass.class, m.getId(), 
                 new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)
   )
);

Or

public List<MyClass> getMyClassObjects(Session session) {
   final Query query = session.createQuery("from MyClass m");
   query.setLockMode("m", LockMode.PESSIMISTIC_FORCE_INCREMENT);
   return query.list();
}
KayV
  • 12,987
  • 11
  • 98
  • 148