# Error while persisting an object in Hibernate 6

**URL:** https://discourse.hibernate.org/t/error-while-persisting-an-object-in-hibernate-6/10077
**Category:** Hibernate ORM
**Created:** [August 6, 2024, 6:10am UTC](https://discourse.hibernate.org/t/error-while-persisting-an-object-in-hibernate-6/10077 "2024-08-06T06:10:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Kaushik\_LV](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/kaushik_lv/32/3312_2.png) [@Kaushik\_LV](https://discourse.hibernate.org/u/Kaushik_LV)
#### Post date: [August 6, 2024, 6:10am UTC](https://discourse.hibernate.org/t/error-while-persisting-an-object-in-hibernate-6/10077/1 "2024-08-06T06:10:04Z")

</div>

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 -

```auto
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 -

```auto
entityManager.persist(object);

```

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

```auto
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?

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [August 6, 2024, 2:31pm UTC](https://discourse.hibernate.org/t/error-while-persisting-an-object-in-hibernate-6/10077/2 "2024-08-06T14:31:08Z")

</div>

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`](https://docs.jboss.org/hibernate/orm/6.5/userguide/html_single/Hibernate_User_Guide.html#events) to help Hibernate resolve the entity name from an object via `Interceptor#getEntityName`.

---

<div class="post-metadata">

### Author: ![Kaushik\_LV](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/kaushik_lv/32/3312_2.png) [@Kaushik\_LV](https://discourse.hibernate.org/u/Kaushik_LV)
#### Post date: [August 12, 2024, 6:21am UTC](https://discourse.hibernate.org/t/error-while-persisting-an-object-in-hibernate-6/10077/3 "2024-08-12T06:21:37Z")

</div>

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 -

```auto
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 -

```auto
configuration.addEntityNameResolver(new HibernateEntityNameResolver());

```

I still see the same error right now.

```auto
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 ?

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [August 13, 2024, 12:06pm UTC](https://discourse.hibernate.org/t/error-while-persisting-an-object-in-hibernate-6/10077/4 "2024-08-13T12:06:59Z")

</div>

Please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.

---

<div class="post-metadata">

### Author: ![Kaushik\_LV](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/kaushik_lv/32/3312_2.png) [@Kaushik\_LV](https://discourse.hibernate.org/u/Kaushik_LV)
#### Post date: [August 14, 2024, 4:19pm UTC](https://discourse.hibernate.org/t/error-while-persisting-an-object-in-hibernate-6/10077/5 "2024-08-14T16:19:56Z")

</div>

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](https://hibernate.atlassian.net/browse/HHH-18486)

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