From 1ae633b5c669a3ff0139a177d9d17daf4f28abf4 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Thu, 22 Aug 2024 10:38:44 +0200 Subject: [PATCH] HHH-18486 Add test for issue --- .../CustomEntityNameResolverTest.java | 58 +++++++++++++++++++ .../orm/test/dynamicmap/artist.hbm.xml | 9 +++ 2 files changed, 67 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/dynamicmap/CustomEntityNameResolverTest.java create mode 100644 hibernate-core/src/test/resources/org/hibernate/orm/test/dynamicmap/artist.hbm.xml diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/dynamicmap/CustomEntityNameResolverTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/dynamicmap/CustomEntityNameResolverTest.java new file mode 100644 index 0000000000..744674c5d9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/dynamicmap/CustomEntityNameResolverTest.java @@ -0,0 +1,58 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.dynamicmap; + +import java.util.HashMap; +import java.util.Map; + +import org.hibernate.EntityNameResolver; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Configuration; +import org.hibernate.engine.spi.SessionFactoryImplementor; + +import org.hibernate.testing.orm.junit.Jira; +import org.junit.jupiter.api.Test; + +import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction; +import static org.hibernate.tool.schema.Action.ACTION_CREATE_THEN_DROP; + +/** + * @author Marco Belladelli + */ +@Jira( "https://hibernate.atlassian.net/browse/HHH-18486" ) +public class CustomEntityNameResolverTest { + @Test + public void test() { + final Configuration configuration = new Configuration(); + configuration.setProperty( AvailableSettings.HBM2DDL_AUTO, ACTION_CREATE_THEN_DROP ); + configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() ); + configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() ); + configuration.addResource( "org/hibernate/orm/test/dynamicmap/artist.hbm.xml" ); + configuration.addEntityNameResolver( new HibernateEntityNameResolver() ); + try (final SessionFactoryImplementor sf = (SessionFactoryImplementor) configuration.buildSessionFactory()) { + inTransaction( sf, session -> { + final Map artistEntity = new HashMap<>(); + artistEntity.put( "id", 1 ); + artistEntity.put( "firstname", "John" ); + artistEntity.put( "lastname", "Doe" ); + // Persist the dynamic map entity + session.persist( artistEntity ); + } ); + sf.getSchemaManager().truncateMappedObjects(); + } + } + + static class HibernateEntityNameResolver implements EntityNameResolver { + @Override + public String resolveEntityName(Object entity) { + if ( entity instanceof Map ) { + return "artist"; + } + return null; + } + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/dynamicmap/artist.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/dynamicmap/artist.hbm.xml new file mode 100644 index 0000000000..590155b72d --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/dynamicmap/artist.hbm.xml @@ -0,0 +1,9 @@ + + + + + + + +