From b69c554f9eb9f6a367dca7ab92e7722f5d67ce4f Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 12 Nov 2012 14:21:01 -0600 Subject: [PATCH] HHH-7643 - java.io.NotSerializableException org.hibernate.type.EnumType (cherry picked from commit 2158bec47c5a8fa80613442c83e76464fcc36569) --- .../java/org/hibernate/type/EnumType.java | 6 +- .../test/enums/TestEnumTypeSerialization.java | 66 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/enums/TestEnumTypeSerialization.java 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 f4a572960c..d2868b2e5b 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 class EnumType implements EnhancedUserType, DynamicParameterizedType, Ser 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 class EnumType implements EnhancedUserType, DynamicParameterizedType, Ser } } - private class OrdinalEnumValueMapper extends EnumValueMapperSupport implements EnumValueMapper { + private class OrdinalEnumValueMapper extends EnumValueMapperSupport implements EnumValueMapper, Serializable { private transient Enum[] enumsByOrdinal; @Override @@ -416,7 +416,7 @@ public class EnumType implements EnhancedUserType, DynamicParameterizedType, Ser } } - 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 0000000000..446dd73b7a --- /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 ); + } + } +}