Issue
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query updateQuery = session.createQuery("UPDATE Lot l SET l.currentRate = l.currentRate + 100, l.lastOwner = :lastowner WHERE l.id = :lotid", null);
updateQuery.setLockMode(LockModeType.PESSIMISTIC_WRITE);
So, here is my code fragment. I got an exception: java.lang.IllegalStateException: Expecting a SELECT query : UPDATE Lot l SET l.currentRate = l.currentRate + 100, l.lastOwner = :lastowner WHERE l.id = :lotid
when I try to call setLockMode.
Why? Am I doing something wrong?
Solution
See the Java doc of the method you are trying to call: https://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#setLockMode(javax.persistence.LockModeType)
It explicitly says, that an IllegalStateException
will be thrown if you execute this on a non-SELECT statement.
Answered By - Christian Beikov
Answer Checked By - Gilberto Lyons (JavaFixing Admin)