I have a server mistake:
> ERROR [http-nio-8080-exec-5]
> org.hibernate.internal.ExceptionMapperStandardImpl.mapManagedFlushFailure
> HHH000346: Error during managed flush [Row was updated or deleted by
> another transaction (or unsaved-value mapping was incorrect) :
> [com.javaschool.ev.domain.Train#0]]
in browser in looks like this:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Request processing failed; nested exception is org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException: Object of class [com.javaschool.ev.domain.Train] with identifier [0]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.javaschool.ev.domain.Train#0]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException: Object of class [com.javaschool.ev.domain.Train] with identifier [0]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.javaschool.ev.domain.Train#0]
I miss Optimistic Locking (Baeldung), though for versioned entities optimistic locking is available by default.
How and where I can request optimistic locking explicitly?
I have tried to do it in TrainDAOImpl, but IDE cannot resolve OPTIMISTIC_INCREMENT.(code is below)
@Override
public boolean checkTrain(int number, int availableSeats, int bookedSeats, String occurence) {
Session session = sessionFactory.getCurrentSession();
Query query;
query = session.createQuery("from Train where number = :number");
query.setParameter("number", number);
query = session.createQuery("from Train where availableSeats = :availableSeats");
query.setParameter("availableSeats", availableSeats);
query = session.createQuery("from Train where bookedSeats = :bookedSeats");
query.setParameter("bookedSeats", bookedSeats);
query = session.createQuery("from Train where occurence = :occurence");
query.setParameter("occurence", occurence);
query.setLockMode(LockModeType.OPTIMISTIC_INCREMENT);
return query.list().isEmpty();
}
Here is my project in Github
Thank you very much in advance!
Cheers, Kateridzhe