diff --git a/hibernate-core/src/main/java/org/hibernate/type/EnumType.java b/hibernate-core/src/main/java/org/hibernate/type/EnumType.java index 9bf0a39ab9..e4c3e6a5ca 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/EnumType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/EnumType.java @@ -156,7 +156,7 @@ public class EnumType implements EnhancedUserType, DynamicParameterizedType,Logg return new OrdinalEnumValueMapper(); } else if ( isCharacterType( type ) ) { - return new OrdinalEnumValueMapper(); + return new NamedEnumValueMapper(); } else { throw new HibernateException( diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/EnumExplicitTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/enums/EnumExplicitTypeTest.java new file mode 100644 index 0000000000..6371671efd --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/EnumExplicitTypeTest.java @@ -0,0 +1,46 @@ +/* + * 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 . + */ +package org.hibernate.test.enums; + +import org.hibernate.Session; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea + */ +public class EnumExplicitTypeTest extends BaseCoreFunctionalTestCase { + + protected String[] getMappings() { + return new String[] { "enums/Person.hbm.xml" }; + } + + @Test + @TestForIssue(jiraKey = "HHH-10766") + public void hbmEnumWithExplicitTypeTest() { + Session s = openSession(); + s.getTransaction().begin(); + Person painted = Person.person( Gender.MALE, HairColor.BROWN ); + painted.setOriginalHairColor( HairColor.BLONDE ); + s.persist( painted ); + s.getTransaction().commit(); + s.clear(); + + s.getTransaction().begin(); + Number id = (Number) session.createNativeQuery( + "select id from Person where originalHairColor = :color" ) + .setParameter( "color", HairColor.BLONDE.name() ) + .getSingleResult(); + assertEquals( 1L, id.longValue() ); + s.getTransaction().commit(); + s.close(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java index b76de5e733..afc58d9f34 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java @@ -23,7 +23,7 @@ public class EnumTypeTest extends BaseCoreFunctionalTestCase { protected String[] getMappings() { return new String[] { "enums/Person.hbm.xml" }; } - + @Test @TestForIssue(jiraKey = "HHH-8153") public void hbmEnumTypeTest() { @@ -35,7 +35,7 @@ public class EnumTypeTest extends BaseCoreFunctionalTestCase { s.persist( Person.person( Gender.FEMALE, HairColor.BLACK ) ); s.getTransaction().commit(); s.clear(); - + s.getTransaction().begin(); assertEquals(s.createCriteria( Person.class ) .add( Restrictions.eq( "gender", Gender.MALE ) ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/Person.java b/hibernate-core/src/test/java/org/hibernate/test/enums/Person.java index 545697bc9e..ebd5e2fc4e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/enums/Person.java +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/Person.java @@ -13,6 +13,8 @@ public class Person { private HairColor hairColor; + private HairColor originalHairColor; + public static Person person(Gender gender, HairColor hairColor) { Person person = new Person(); person.setGender( gender ); @@ -43,4 +45,12 @@ public class Person { public void setHairColor(HairColor hairColor) { this.hairColor = hairColor; } + + public HairColor getOriginalHairColor() { + return originalHairColor; + } + + public void setOriginalHairColor(HairColor originalHairColor) { + this.originalHairColor = originalHairColor; + } } diff --git a/hibernate-core/src/test/resources/org/hibernate/test/enums/Person.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/enums/Person.hbm.xml index 470afc1182..6b4b1100a7 100644 --- a/hibernate-core/src/test/resources/org/hibernate/test/enums/Person.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/test/enums/Person.hbm.xml @@ -14,16 +14,26 @@ + org.hibernate.test.enums.Gender 12 + org.hibernate.test.enums.HairColor + + + + org.hibernate.test.enums.HairColor + 12 + + + \ No newline at end of file