1

I would like to update @Version attribute of my entity without any changes in entity. This entity has as a child collections of another entity. And I would like to update the basic entity with @Version attribute to get new value of this entity version under Hibernate control. I have something like that (attribute isn't attribute with @Version annotation):

MyEntity entity = (MyEntity) getSession().load(Entity.class, id);
entity.setAttribute(new String(entity.getAttribute()));
getSession().update(entity);

But this is not working. The entiy has new version value only in case I am setting new value to one of its attributes. How can I load and update entity to increment Hibernate @Version attribute without any changes in that case?

1
  • Just curious. why do you the version attribute to be increased? Commented Apr 6, 2013 at 21:53

1 Answer 1

1

In fact you should have a version increment by doing a locking om your entity with a LockMode.FORCE:

Session session = getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
MyEntity u = (MyEntity) session.get(MyEntity.class, id);
session.lock(u, LockMode.FORCE);
tx.commit();
session.close();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. However lock method is deprecated I have used something like that: session.buildLockRequest(LockOptions.UPGRADE).setLockMode(LockMode.PESSIMISTIC_‌​FORCE_INCREMENT).lock(u); But unfortunately I am getting this exception: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.