Error while persisting an object in Hibernate 6

Hi,
We are trying to migrate to Hibernate 6.5 and as part of this we have changed the implementation from using Sessions to EntityManagers.

Previously while inserting a record we were using the Session class’ save method as follows -

session.save(entityName, object);

Where object was an implementation of HibernateProxy. It was working fine.

Now, in Hibernate 6 we are using EntityManager’s persist method to accomplish the same task as follows -

entityManager.persist(object);

And here as well the type of object is an implementation of HibernateProxy itself
But we are seeing this exception -

org.hibernate.HibernateException: Could not determine type of dynamic map entity
	at org.hibernate.metamodel.internal.EntityInstantiatorDynamicMap.extractEmbeddedEntityName(EntityInstantiatorDynamicMap.java:72)
	at org.hibernate.metamodel.internal.EntityInstantiatorDynamicMap.lambda$static$0(EntityInstantiatorDynamicMap.java:59)
	at org.hibernate.internal.CoordinatingEntityNameResolver.resolveEntityName(CoordinatingEntityNameResolver.java:35)
	at org.hibernate.internal.SessionImpl.guessEntityName(SessionImpl.java:1805)
	at org.hibernate.internal.SessionImpl.bestGuessEntityName(SessionImpl.java:1756)
	at org.hibernate.event.internal.DefaultPersistEventListener.entityName(DefaultPersistEventListener.java:142)
	at org.hibernate.event.internal.DefaultPersistEventListener.persist(DefaultPersistEventListener.java:84)
	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:77)
	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:54)
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
	at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:757)
	at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:741)

Is there any change in behaviour w.r.t saving / persisting the object in Hibernate 6?

Using persist directly also wouldn’t have worked in previous Hibernate ORM versions. You will have to add a custom EntityNameResolver via org.hibernate.cfg.Configuration#addEntityNameResolver or implement a custom org.hibernate.Interceptor to help Hibernate resolve the entity name from an object via Interceptor#getEntityName.

Hi @beikov

I have tried using the custom EntityNameResolver via org.hibernate.cfg.Configuration#addEntityNameResolver method.
Here is what I have done exactly -
Created this class CustomEntityResolver -

public class CustomEntityNameResolver implements EntityNameResolver {
    @Override
    public String resolveEntityName(Object entity) {
        if(entity instanceof CustomHibernateProxy)
            return ((CustomHibernateProxy) entity).getEntityName();
        return null;
    }

    public boolean equals(Object obj) {
        return getClass().equals( obj.getClass() );
    }

    public int hashCode() {
        return getClass().hashCode();
    }
}

I have added this Resolver to my configuration before creating the EntityManagerFactory -

configuration.addEntityNameResolver(new HibernateEntityNameResolver());

I still see the same error right now.

org.hibernate.HibernateException: Could not determine type of dynamic map entity

Is there anything else I need to add before calling the persist method using the entityManager ?

Please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.

Hi @beikov

As requested, I have created a reproducer and raised a bug in your issue tracker. Here is the link to the bug -
https://hibernate.atlassian.net/browse/HHH-18486

Hope you have all the information that you need to resolve this issue.

1 Like