Skip to content

Commit

Permalink
HHH-7643 - java.io.NotSerializableException org.hibernate.type.EnumType
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Nov 12, 2012
1 parent 07fc162 commit 2158bec
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
6 changes: 3 additions & 3 deletions hibernate-core/src/main/java/org/hibernate/type/EnumType.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
@@ -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 );
}
}
}

0 comments on commit 2158bec

Please sign in to comment.