Hibernate calls flush on find - causes a non-null error

I have a process that updates the tree in the database while doing a read to check for duplicate entities.

I find that trying to fulfill the .uniqueResult () criterion halfway through this process causes the following error:

org.hibernate.PropertyValueException: null-null property refers to null or a transition value

Digging into the stack trace, I see that uniqueResult () clears the session, trying to perform updates that are not yet ready to work in the database.

at org.hibernate.engine.Cascade.cascade(Cascade.java:153)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:145)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:58)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:996)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1589)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:328)
at com.inversion.dal.BaseDAO.findUniqueByCriterion(BaseDAO.java:59)

Have I installed something wrong here?

Any help is greatly appreciated.

Marty

+3
source share
3 answers

hibernate , . select, hibernate . , .

flushmode - , FlushMode.AUTO, . , . , .

+3

Session, .

Session s;
// if you're doing transactional work
s.setFlushMode(FlushMode.COMMIT);
// if you want to control flushes directly
s.setFlushMode(FlushMode.MANUAL);

. - , , .

+2

, . , , .

If you are nearing your end, you can also execute supporting requests, such as findAllin a new session:

Domain.withNewSession { session -> ... }

This usually got around the problem for me in most cases.

0
source

Source: https://habr.com/ru/post/1708909/


All Articles