HHH-14820 - Customized EnumType not working with hbm mapping in Hibernate 6

This commit is contained in:
Steve Ebersole 2021-09-30 11:53:51 -05:00
parent bef0554eb7
commit c799f85801
6 changed files with 105 additions and 75 deletions

View File

@ -102,7 +102,7 @@ public class TypeDefinition implements Serializable {
if ( CollectionHelper.isEmpty( localConfigParameters ) ) {
// we can use the re-usable resolution...
if ( reusableResolution == null ) {
reusableResolution = createResolution( null, Collections.emptyMap(), indicators, context );
reusableResolution = createResolution( name, Collections.emptyMap(), indicators, context );
}
return reusableResolution;

View File

@ -1,50 +0,0 @@
/**
*
*/
package org.hibernate.orm.test.mapping.converted.enums;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Ignore;
import org.junit.Test;
/**
* @author gtoison
*
*/
public class NamedEnumUserTypeTest extends BaseCoreFunctionalTestCase {
@Override
protected String getBaseForMappings() {
return "";
}
@Override
protected String[] getMappings() {
return new String[] { "org/hibernate/orm/test/mapping/converted/enums/PersonNamedEnumsUserType.xml" };
}
@Override
protected void prepareTest() {
doInHibernate( this::sessionFactory, s -> {
s.persist( Person.person( Gender.MALE, HairColor.BLACK ) );
} );
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Ignore
@Test
@TestForIssue(jiraKey = "HHH-14820")
public void testNamedEnumUserType() {
// This fails because the same instance of NamedEnumUserType is used for both enums
doInHibernate( this::sessionFactory, s -> {
s.createQuery( "from Person p", Person.class );
} );
}
}

View File

@ -1,21 +1,27 @@
package org.hibernate.orm.test.mapping.converted.enums;
import java.util.Properties;
import org.hibernate.type.EnumType;
/**
* A simple user type where we force enums to be saved by name, not ordinal
*
* @author gtoison
*/
public class NamedEnumUserType<T extends Enum<T>> extends EnumType<T> {
private static final long serialVersionUID = -4176945793071035928L;
@Override
public void setParameterValues(Properties parameters) {
parameters.setProperty(EnumType.NAMED, "true");
super.setParameterValues(parameters);
}
/*
* 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.mapping.type.custom.typedef;
import java.util.Properties;
import org.hibernate.type.EnumType;
/**
* A simple user type where we force enums to be saved by name, not ordinal
*
* @author gtoison
*/
public class NamedEnumUserType<T extends Enum<T>> extends EnumType<T> {
private static final long serialVersionUID = -4176945793071035928L;
@Override
public void setParameterValues(Properties parameters) {
parameters.setProperty(EnumType.NAMED, "true");
super.setParameterValues(parameters);
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.mapping.type.custom.typedef;
import org.hibernate.orm.test.mapping.converted.enums.Gender;
import org.hibernate.orm.test.mapping.converted.enums.HairColor;
import org.hibernate.orm.test.mapping.converted.enums.Person;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Tests that the same UserType class used in multiple distinct TypeDefinitions
* get distinct ManagedBeans.
*
* NOTE : this is with no local parameter
*
* @author gtoison
*/
@DomainModel(
xmlMappings = "mappings/type/custom/typedef/PersonNamedEnumsUserType.xml"
)
@SessionFactory
public class NamedEnumUserTypeTest {
@Test
@TestForIssue(jiraKey = "HHH-14820")
public void testNamedEnumUserType(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "from Person p", Person.class ).list();
} );
}
@BeforeEach
public void createTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.persist( Person.person( Gender.MALE, HairColor.BLACK ) );
} );
}
@AfterEach
public void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.createQuery( "delete Person" ).executeUpdate();
} );
}
}

View File

@ -0,0 +1,13 @@
/*
* 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
*/
/**
* Tests for {@code <type-def/>} definitions in {@code hbm.xml} mappings
*
* @author Steve Ebersole
*/
package org.hibernate.orm.test.mapping.type.custom.typedef;

View File

@ -2,18 +2,18 @@
<!--
~ 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>.
~ 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
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<typedef name="gender" class="org.hibernate.orm.test.mapping.converted.enums.NamedEnumUserType">
<typedef name="gender" class="org.hibernate.orm.test.mapping.type.custom.typedef.NamedEnumUserType">
<param name="enumClass">org.hibernate.orm.test.mapping.converted.enums.Gender</param>
</typedef>
<typedef name="hairColor" class="org.hibernate.orm.test.mapping.converted.enums.NamedEnumUserType">
<typedef name="hairColor" class="org.hibernate.orm.test.mapping.type.custom.typedef.NamedEnumUserType">
<param name="enumClass">org.hibernate.orm.test.mapping.converted.enums.HairColor</param>
</typedef>