'org.hibernate.internal.ExceptionMapperStandardImpl.mapManagedFlushFailure HHH000346: Error during managed flush

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



Solution 1:[1]

There is no project on Github.

Here is the general solution.

If you try to do something to one field and it affects another which is an invalid update then it raises this error.

Like if there is Bi-directional mapping and if you try to delete or do something first you need to break the mapping to that doing something does not affect another.

To break the link you need to put null in one fields setter.

Like this:

tempInstructorDetail.getInstructor().setInstructorDetail(null);

Then you can delete or do something to one while keeping the other intact.

Hope this solves your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1