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();
		}
	}

}

It’s not clear what you are doing there. Try to isolate the problem using this test case template.

If you cannot replicate it, it might be caused by something else on your stack, like Seam for instance.

I tested Hibernate 5.3.4, 5.3.3 and 5.2.17 in all 3 versions the compressedState is 17. Now I tested Hibernate 5.1.15.Final the compressedState is 8209 and everythink works like a charm. So I think the compressedState should be 8209. But how can I change it? Somethink must be wrong at the stage persist, flush, commit or refresh? What is the meaning of compressedState 8209 or 17? I found out the meaning of 8192 is EXISTS_IN_DATABASE(13). I would like to use the newest Hibernate version and not stuck at 5.1. :disappointed_relieved:.

I tried to reprodruce this error with somethink simple like this:

EntityManager entityManager = this.entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();	
Entity entity = new Entity();
entityManager.persist(entity);
entityManager.flush();
entityManager.getTransaction().commit();
entityManager.refresh(entity);
entityManager.close();

I’m getting my error at "entityManager.refresh(entity); " With your Testcase it is not reproduceable, I’m not getting this error in all version (5.3.4, 5.3.3, 5.2.17,5.1.15). I only get the Error with using Seam :cry:, except in Version 5.1.15 it is working.

Most likely this is a Seam issue, and Seam has been discontinued for a long time now.

You might want to migrate to a Java EE or Spring alternative if you want to use the latest versions of frameworks that Seam used.

Otherwise, you might bump into other issues, not just this one.

Yes I do want to Upgrade, but it is not possible now. Maybe in 3 Years. The application is to heavy and not all features are implemented. I wanted to start step by step, now I start with hibernate. Maybe later to Spring …

Just like Spring or Java EE, Seam used all sorts of integration points for Hibernate as well. If you want to upgrade to a newer version like 5.3, it might be that you would need to investigate those and change Seam code to accommodate the new API changes.

I tried some more Hibernate version. Hibernate 5.2.17 does not has this problem also, so I can use this Version. I dont know why it didnt worked in the first time. Maybe some maven clean, caching or somethink didnt worked. I also tried all Hibernate version from 5.3.0.Beta1 till 5.3.4, but all Hibernate 5.3.x Version dont work. So it could have somethink to do with JPA 2.2, or some other improvements from Hibernate 5.3. Im going checkout every new Hibernate Version. Maybe sometime it is going to be fixed. I just can’t reproduce the Bug without seam, right now.

I tried every Hibernate Version from 5.3.X till 5.4.X. The Bug got fixed with Hibernate Version 5.3.8. and 5.4.1. In earlier Versions the Bug still exist. Thanks! Now we are able to use the newest Hibernate Version 5.4.2 with Seam 2.2.2. We are going to replace Seam but not right now.