Skip to content

Commit 1ae633b

Browse files
committed
HHH-18486 Add test for issue
1 parent edc7b5d commit 1ae633b

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
2+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
3+
<hibernate-mapping>
4+
<class entity-name="artist" lazy="true" table="ARTISTS">
5+
<id name="id" type="int" />
6+
<property name="firstname" type="string" />
7+
<property name="lastname" type="string" />
8+
</class>
9+
</hibernate-mapping>

0 commit comments

Comments
 (0)