|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html |
| 6 | + */ |
| 7 | +package org.hibernate.orm.test.dynamicmap; |
| 8 | + |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import org.hibernate.EntityNameResolver; |
| 13 | +import org.hibernate.cfg.AvailableSettings; |
| 14 | +import org.hibernate.cfg.Configuration; |
| 15 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 16 | + |
| 17 | +import org.hibernate.testing.orm.junit.Jira; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import static org.hibernate.testing.transaction.TransactionUtil2.inTransaction; |
| 21 | +import static org.hibernate.tool.schema.Action.ACTION_CREATE_THEN_DROP; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Marco Belladelli |
| 25 | + */ |
| 26 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-18486" ) |
| 27 | +public class CustomEntityNameResolverTest { |
| 28 | + @Test |
| 29 | + public void test() { |
| 30 | + final Configuration configuration = new Configuration(); |
| 31 | + configuration.setProperty( AvailableSettings.HBM2DDL_AUTO, ACTION_CREATE_THEN_DROP ); |
| 32 | + configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() ); |
| 33 | + configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() ); |
| 34 | + configuration.addResource( "org/hibernate/orm/test/dynamicmap/artist.hbm.xml" ); |
| 35 | + configuration.addEntityNameResolver( new HibernateEntityNameResolver() ); |
| 36 | + try (final SessionFactoryImplementor sf = (SessionFactoryImplementor) configuration.buildSessionFactory()) { |
| 37 | + inTransaction( sf, session -> { |
| 38 | + final Map<String, Object> artistEntity = new HashMap<>(); |
| 39 | + artistEntity.put( "id", 1 ); |
| 40 | + artistEntity.put( "firstname", "John" ); |
| 41 | + artistEntity.put( "lastname", "Doe" ); |
| 42 | + // Persist the dynamic map entity |
| 43 | + session.persist( artistEntity ); |
| 44 | + } ); |
| 45 | + sf.getSchemaManager().truncateMappedObjects(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static class HibernateEntityNameResolver implements EntityNameResolver { |
| 50 | + @Override |
| 51 | + public String resolveEntityName(Object entity) { |
| 52 | + if ( entity instanceof Map ) { |
| 53 | + return "artist"; |
| 54 | + } |
| 55 | + return null; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments