diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java index accfc0a047f0..f0fbb6dcbb2a 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java @@ -370,7 +370,9 @@ private void createParameterImpl() { final XProperty xProperty = (XProperty) typeParameters.get( DynamicParameterizedType.XPROPERTY ); // todo : not sure this works for handling @MapKeyEnumerated - final Annotation[] annotations = xProperty.getAnnotations(); + final Annotation[] annotations = xProperty == null + ? null + : xProperty.getAnnotations(); typeParameters.put( DynamicParameterizedType.PARAMETER_TYPE, @@ -383,7 +385,8 @@ private void createParameterImpl() { table.getSchema(), table.getName(), Boolean.valueOf( typeParameters.getProperty( DynamicParameterizedType.IS_PRIMARY_KEY ) ), - columnsNames ) + columnsNames + ) ); } catch ( ClassNotFoundException cnfe ) { 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 284f2da6b0a0..f4a572960c1b 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/EnumType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/EnumType.java @@ -261,12 +261,14 @@ else if ( javax.persistence.EnumType.STRING.equals( enumType ) ) { private void treatAsOrdinal() { if ( enumValueMapper == null || ! OrdinalEnumValueMapper.class.isInstance( enumValueMapper ) ) { enumValueMapper = new OrdinalEnumValueMapper(); + sqlType = enumValueMapper.getSqlType(); } } private void treatAsNamed() { if ( enumValueMapper == null || ! NamedEnumValueMapper.class.isInstance( enumValueMapper ) ) { enumValueMapper = new NamedEnumValueMapper(); + sqlType = enumValueMapper.getSqlType(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/UnspecifiedEnumTypeEntity.java b/hibernate-core/src/test/java/org/hibernate/test/enums/UnspecifiedEnumTypeEntity.java new file mode 100644 index 000000000000..f69488f12500 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/UnspecifiedEnumTypeEntity.java @@ -0,0 +1,82 @@ +package org.hibernate.test.enums; + +import java.io.Serializable; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class UnspecifiedEnumTypeEntity implements Serializable { + public static enum E1 { X, Y } + public static enum E2 { A, B } + + private Long id; + + private E1 enum1; + + private E2 enum2; + + public UnspecifiedEnumTypeEntity() { + } + + public UnspecifiedEnumTypeEntity(E1 enum1, E2 enum2) { + this.enum1 = enum1; + this.enum2 = enum2; + } + + public UnspecifiedEnumTypeEntity(E1 enum1, E2 enum2, Long id) { + this.enum1 = enum1; + this.enum2 = enum2; + this.id = id; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( ! ( o instanceof UnspecifiedEnumTypeEntity ) ) return false; + + UnspecifiedEnumTypeEntity that = (UnspecifiedEnumTypeEntity) o; + + if ( enum1 != that.enum1 ) return false; + if ( enum2 != that.enum2 ) return false; + if ( id != null ? !id.equals( that.id ) : that.id != null ) return false; + + return true; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + ( enum1 != null ? enum1.hashCode() : 0 ); + result = 31 * result + ( enum2 != null ? enum2.hashCode() : 0 ); + return result; + } + + @Override + public String toString() { + return "UnspecifiedEnumTypeEntity(id = " + id + ", enum1 = " + enum1 + ", enum2 = " + enum2 + ")"; + } + + public E1 getEnum1() { + return enum1; + } + + public void setEnum1(E1 enum1) { + this.enum1 = enum1; + } + + public E2 getEnum2() { + return enum2; + } + + public void setEnum2(E2 enum2) { + this.enum2 = enum2; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/UnspecifiedEnumTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/enums/UnspecifiedEnumTypeTest.java new file mode 100644 index 000000000000..84a9f1e0000f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/UnspecifiedEnumTypeTest.java @@ -0,0 +1,97 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2012, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.enums; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@TestForIssue( jiraKey = "HHH-7780" ) +@RequiresDialect( value = H2Dialect.class ) +public class UnspecifiedEnumTypeTest extends BaseCoreFunctionalTestCase { + @Override + protected String[] getMappings() { + return new String[] { "enums/mappings.hbm.xml" }; + } + + @Override + protected void configure(Configuration configuration) { + super.configure( configuration ); + configuration.setProperty( Environment.HBM2DDL_AUTO, "" ); + } + + @Before + public void prepareTable() { + Session session = openSession(); + dropTable( session ); + createTable( session ); + session.close(); + } + + public void dropTable(Session session) { + executeUpdateSafety( session, "drop table ENUM_ENTITY if exists" ); + } + + private void createTable(Session session) { + executeUpdateSafety( + session, + "create table ENUM_ENTITY (ID bigint not null, enum1 varchar(255), enum2 integer, primary key (ID))" + ); + } + + @After + public void dropTable() { + dropTable( session ); + } + + @Test + public void testEnumTypeDiscovery() { + Session session = openSession(); + session.beginTransaction(); + UnspecifiedEnumTypeEntity entity = new UnspecifiedEnumTypeEntity( UnspecifiedEnumTypeEntity.E1.X, UnspecifiedEnumTypeEntity.E2.A ); + session.persist( entity ); + session.getTransaction().commit(); + session.close(); + } + + private void executeUpdateSafety(Session session, String query) { + try { + session.createSQLQuery( query ).executeUpdate(); + } + catch ( Exception e ) { + } + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/test/enums/mappings.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/test/enums/mappings.hbm.xml new file mode 100644 index 000000000000..3000e3fb8f95 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/enums/mappings.hbm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + org.hibernate.test.enums.UnspecifiedEnumTypeEntity$E1 + + + + + + org.hibernate.test.enums.UnspecifiedEnumTypeEntity$E2 + + + +