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 f4a572960c1b..d2868b2e5b2b 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/EnumType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/EnumType.java @@ -313,7 +313,7 @@ public Object fromXMLString(String xmlValue) { return enumValueMapper.fromXMLString( xmlValue ); } - private static interface EnumValueMapper { + private static interface EnumValueMapper extends Serializable { public int getSqlType(); public Enum getValue(ResultSet rs, String[] names) throws SQLException; public void setValue(PreparedStatement st, Enum value, int index) throws SQLException; @@ -345,7 +345,7 @@ public void setValue(PreparedStatement st, Enum value, int index) throws SQLExce } } - private class OrdinalEnumValueMapper extends EnumValueMapperSupport implements EnumValueMapper { + private class OrdinalEnumValueMapper extends EnumValueMapperSupport implements EnumValueMapper, Serializable { private transient Enum[] enumsByOrdinal; @Override @@ -416,7 +416,7 @@ protected Object extractJdbcValue(Enum value) { } } - private class NamedEnumValueMapper extends EnumValueMapperSupport implements EnumValueMapper { + private class NamedEnumValueMapper extends EnumValueMapperSupport implements EnumValueMapper, Serializable { @Override public int getSqlType() { return Types.VARCHAR; diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/TestEnumTypeSerialization.java b/hibernate-core/src/test/java/org/hibernate/test/enums/TestEnumTypeSerialization.java new file mode 100644 index 000000000000..446dd73b7a2b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/TestEnumTypeSerialization.java @@ -0,0 +1,66 @@ +/* + * 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 java.util.Properties; + +import org.hibernate.internal.util.SerializationHelper; +import org.hibernate.type.EnumType; +import org.hibernate.usertype.DynamicParameterizedType; + +import org.junit.Test; + +import org.hibernate.testing.junit4.BaseUnitTestCase; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Steve Ebersole + */ +public class TestEnumTypeSerialization extends BaseUnitTestCase { + @Test + public void testSerializability() { + { + // test ordinal mapping + EnumType enumType = new EnumType(); + Properties properties = new Properties(); + properties.put( EnumType.ENUM, UnspecifiedEnumTypeEntity.E1.class.getName() ); + enumType.setParameterValues( properties ); + assertTrue( enumType.isOrdinal() ); + SerializationHelper.clone( enumType ); + } + + { + // test named mapping + EnumType enumType = new EnumType(); + Properties properties = new Properties(); + properties.put( EnumType.ENUM, UnspecifiedEnumTypeEntity.E1.class.getName() ); + properties.put( EnumType.NAMED, "true" ); + enumType.setParameterValues( properties ); + assertFalse( enumType.isOrdinal() ); + SerializationHelper.clone( enumType ); + } + } +}