Versionning problem in case of autoFlush required inside an "onSave" interceptor


I have an interceptor "onSave" for an Entity.
I try to make a query with hibernate inside this onSave.
The following exception is thrown :

at org.hibernate.engine.Versioning.increment(
at org.hibernate.event.def.DefaultFlushEntityEventListener.getNextVersion(
at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(
at org.hibernate.impl.SessionImpl.list(
at org.hibernate.impl.CriteriaImpl.list(
at org.springframework.orm.hibernate3.HibernateTemplate$36.doInHibernate(
at org.springframework.orm.hibernate3.HibernateTemplate$36.doInHibernate(
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(

As you can see, my query triggers an auto flush, because I try to read some objects related to the object currently saved (I cannot access these related objects another way, some relations are not bidirectionnal).
During this auto flush, I have a null pointer inside Versioning.increment because the EntityEntry has a null version, whereas my entity has a version.
I think that the version inside my own entity is not already reported inside the EntityEntry - the proof is in the stack trace before the onSave call:

at org.hibernate.event.def.AbstractSaveEventListener.substituteValuesIfNecessary(
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(
at org.hibernate.event.def.AbstractSaveEventListener.performSave(
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(

As you can see, AbstractSaveEventListener.substituteValuesIfNecessary calls my method. But this method also process versionning :

398 /**
399 * Perform any property value substitution that is necessary
400 * (interceptor callback, version initialization...)
401 *
402 * @param entity The entity
403 * @param id The entity identifier
404 * @param values The snapshot entity state
405 * @param persister The entity persister
406 * @param source The originating session
407 *
408 * @return True if the snapshot state changed such that
409 * reinjection of the values into the entity is required.
410 */
411 protected boolean substituteValuesIfNecessary(
412 Object entity,
413 Serializable id,
414 Object[] values,
415 EntityPersister persister,
416 SessionImplementor source) {
417 boolean substitute = source.getInterceptor().onSave(
418 entity,
419 id,
420 values,
421 persister.getPropertyNames(),
422 persister.getPropertyTypes()
423 );
425 //keep the existing version number in the case of replicate!
426 if ( persister.isVersioned() ) {
427 substitute = Versioning.seedVersion(
428 values,
429 persister.getVersionProperty(),
430 persister.getVersionType(),
431 source
432 ) || substitute;
433 }
434 return substitute;
435 }

"Versioning.seedVersion" is called after "source.getInterceptor().onSave"

Maybe the version should have already been reported inside the EntityEntry when calling onSave ?


Hibernate 3.5.6, Oracle


Romain Fromi
November 7, 2011, 4:25 PM

During the research I made, I saw this entry :

And this post :

So I am not the first one falling into this pit !

Brett Meyer
April 7, 2014, 5:43 PM

In an effort to clean up, in bulk, tickets that are most likely out of date, we're transitioning all ORM 3 tickets to an "Awaiting Test Case" state. Please see for more information.

If this is still a legitimate bug in ORM 4, please provide either a test case that reproduces it or enough detail (entities, mappings, snippets, etc.) to show that it still fails on 4. If nothing is received within 3 months or so, we'll be automatically closing them.

Thank you!

Brett Meyer
July 8, 2014, 3:11 PM

Bulk rejecting stale issues. If this is still a legitimate issue on ORM 4, feel free to comment and attach a test case. I'll address responses case-by-case. Thanks!




Romain Fromi

Fix versions




Suitable for new contributors


Requires Release Note


Pull Request





Affects versions