Search code examples
javahibernatepersistenceevicthibernate-5

Updating to Hibernate 5.1 from 3.6 produce Non-entity object instance passed to evict exception


I was working with a hibernate 3.6 project which use annotations for mapping and now I migrate it to hibernate 5.1 and i have this run time exception Non-entity object instance passed to evict Below is the call to evict

HibernateUtils.getSession().evict(origProject.getProbidinfo());
HibernateUtils.getSession().evict(origProject);

And below is the code from DefaultEvictEventListener.class from where the exception is throwing

EntityPersister persister = null;
                final String entityName = persistenceContext.getSession().guessEntityName( object );
                if ( entityName != null ) {
                    try {
                        persister = persistenceContext.getSession().getFactory().getEntityPersister( entityName );
                    }
                    catch (Exception ignore) {
                    }
                }
                if ( persister == null ) {
                    throw new IllegalArgumentException( "Non-entity object instance passed to evict : " + object );
                }


where persister is null in my case in hibernate 5.1. I can provide further details, if its not clear


Solution

  • The exception "Non-entity object instance passed to evict" was ignored by hibernate until version 4.2, which now throws:

    java.lang.IllegalArgumentException: Non-entity object instance passed to evict.

    In hibernate versions before 4.2, it was ignored by not having an else part for DefaultEvictEventListener.java#L91-L94.

    Starting with version 4.2, the else part was added as we can see at DefaultEvictEventListener.java#L99-L115.

    The only proposal which I find on the web after my research for this bug is that we catch and ignore this exception.

    However, even if its not a perfect solution I think, ommitting the evict call, which cause the exception is what I prefer as I am okay this to be in the cache.