javahibernaterefreshseampersist

Hibernate 5 persist and refresh an entity ends with this instance does not yet exist as a row in the database


I just migrated Hibnernate from Version 3.3.1.GA to Version 5.3.3.Final. Everythink works as it should be, except one think. If I persist an entity, flush and commit it to the database, than the entity realy exist in the Database (I checked it with a select statement). But if I refresh this entity. I'm getting the following exception:

Caused by: javax.persistence.EntityNotFoundException: No row with the given identifier exists: [this instance does not yet exist as a row in the database#14]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.fireRefresh(SessionImpl.java:1336)
    at org.hibernate.internal.SessionImpl.refresh(SessionImpl.java:1301)
    at org.hibernate.internal.SessionImpl.refresh(SessionImpl.java:1295)
    at org.hibernate.internal.SessionImpl.refresh(SessionImpl.java:3625)
    at org.hibernate.internal.SessionImpl.refresh(SessionImpl.java:3601)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.seam.persistence.EntityManagerInvocationHandler.invoke(EntityManagerInvocationHandler.java:46)
    at com.sun.proxy.$Proxy131.refresh(Unknown Source)
    at service.CustomEntityHome.refreshInstance(CustomEntityHome.java:410)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.seam.util.Reflections.invoke(Reflections.java:22)
    at org.jboss.seam.intercept.RootInvocationContext.proceed(RootInvocationContext.java:32)
    at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:56)
    at org.jboss.seam.transaction.RollbackInterceptor.aroundInvoke(RollbackInterceptor.java:28)
    at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
    at org.jboss.seam.core.BijectionInterceptor.aroundInvoke(BijectionInterceptor.java:77)
    at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
    at org.jboss.seam.transaction.TransactionInterceptor$1.work(TransactionInterceptor.java:97)
    at org.jboss.seam.util.Work.workInTransaction(Work.java:61)
    at org.jboss.seam.transaction.TransactionInterceptor.aroundInvoke(TransactionInterceptor.java:91)
    at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
    at org.jboss.seam.core.MethodContextInterceptor.aroundInvoke(MethodContextInterceptor.java:44)
    at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContext.java:68)
    at org.jboss.seam.intercept.RootInterceptor.invoke(RootInterceptor.java:107)
    at org.jboss.seam.intercept.JavaBeanInterceptor.interceptInvocation(JavaBeanInterceptor.java:185)
    at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
    at service.AnredeHome_$$_javassist_seam_46.refreshInstance(AnredeHome_$$_javassist_seam_46.java)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:335)
    ... 62 more

I debuged a little bit here is the Trace:

MutableEntityEntry(AbstractEntityEntry).getCompressedValue(AbstractEntityEntry$BooleanState) line: 516  
MutableEntityEntry(AbstractEntityEntry).isExistsInDatabase() line: 228  
DefaultRefreshEventListener.onRefresh(RefreshEvent, Map) line: 117  
DefaultRefreshEventListener.onRefresh(RefreshEvent) line: 48    
SessionImpl.fireRefresh(RefreshEvent) line: 1306    
SessionImpl.refresh(Object) line: 1257

in line 516 MutableEntityEntry there is a return statemente

 return ( ( compressedState & state.getMask() ) >> state.getOffset() ) == 1;

the variables are:

compressedState=17 == 0001 0001 
(looking in the code)
(0001 the 4. positions means 4 - existsInDatabase ???)    
(What does 17 mean ???)
state.getMask()=8192
state.getOffset()=13

so ==>

( ( 17 & 8192 ) >> 13 ) == 1 
==> False 
==> MutableEntityEntry(AbstractEntityEntry).isExistsInDatabase() line: 228  is false ???WHY???
==> in line 228 MutableEntityEntry 
throw new UnresolvableObjectException(
e.getId(),
"this instance does not yet exist as a row in the database"
);

Why is the compressedState 17? What should it be and how can I change it?

PS: I'm using Java 10 with an old Framework Seam 2.2.2.Final that I cant upgrade it right now. PPS: Seam 2.2.2 is compiled against Hibernate 3, for migration to Hibernate 5 I had to extend two interfaces and a class, because they moved in a differnet package:

the SessionImplementor

package org.hibernate.engine;
public interface SessionImplementor extends org.hibernate.engine.spi.SessionImplementor {}

the EventSource

package org.hibernate.event;
public interface EventSource extends org.hibernate.event.spi.EventSource {}

and a CustomHibernatePersistenceProvider

package org.jboss.seam.persistence;
import javax.persistence.EntityManager;
import org.hibernate.Session;
import org.hibernate.metadata.ClassMetadata;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

@Name("org.jboss.seam.persistence.persistenceProvider")
@Scope(ScopeType.STATELESS)
@BypassInterceptors
@Install(precedence = Install.APPLICATION, classDependencies = { "org.hibernate.Session",
        "javax.persistence.EntityManager" })
public class CustomHibernatePersistenceProvider extends HibernatePersistenceProvider {

    public static Object getVersion(final Object value, final Session session) {
        ClassMetadata classMetadata = getClassMetadata(value, session);
        return classMetadata != null && classMetadata.isVersioned() ? classMetadata.getVersion(value) : null;
    }

    private static ClassMetadata getClassMetadata(final Object value, final Session session) {
        Class entityClass = getEntityClass(value);
        ClassMetadata classMetadata = null;
        if (entityClass != null) {
            classMetadata = session.getSessionFactory().getClassMetadata(entityClass);
            if (classMetadata == null) {
                throw new IllegalArgumentException(
                        "Could not find ClassMetadata object for entity class: " + entityClass.getName());
            }
        }
        return classMetadata;
    }

    @Override
    public Object getVersion(final Object bean, final EntityManager entityManager) {
        try {
            return getVersion(bean, getSession(entityManager));
        } catch (NotHibernateException nhe) {
            return super.getVersion(bean, entityManager);
        }
    }

    private Session getSession(final EntityManager entityManager) {
        Object delegate = entityManager.getDelegate();
        if (delegate instanceof Session) {
            return (Session) delegate;
        } else {
            throw new NotHibernateException();
        }
    }

}

Edit: The Bug got fixed with Hibernate Version 5.3.8. and 5.4.1.!!!


Solution

  • The Bug got fixed with Hibernate Version 5.3.8. and 5.4.1. !!!