hibernate特定用户类型上的java.lang.verifyError
我们一直在使用用于可扩展枚举的,我们的类无法加载到JBoss 6中的Hibernate 3.6+容器中 抛出以下错误hibernate特定用户类型上的java.lang.verifyError,hibernate,Hibernate,我们一直在使用用于可扩展枚举的,我们的类无法加载到JBoss 6中的Hibernate 3.6+容器中 抛出以下错误 #abc state=Create: java.lang.NoSuchMethodError: org.hibernate.type.Type Factory.basic(Ljava/lang/String;)Lorg/hibernate/type/Type; 关于以下代码 type = (NullableType)TypeFactory.basic(identifierTyp
#abc state=Create: java.lang.NoSuchMethodError: org.hibernate.type.Type
Factory.basic(Ljava/lang/String;)Lorg/hibernate/type/Type;
关于以下代码
type = (NullableType)TypeFactory.basic(identifierType.getName());
Hibernate3.6中不再有TypeFactory.basic(字符串)。比较javadocs: http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/type/TypeFactory.html
我认为现在是时候从自定义用户类型转移到标准的@Enumerated:-)不幸的是,如果需要基于枚举的序号或名称以外的内容进行序列化,则@Enumerated不起作用。我设法找到了一个解决方案(从中稍作修改)
import java.io.Serializable;
导入java.lang.reflect.Method;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.util.Properties;
导入org.hibernate.hibernateeexception;
导入org.hibernate.type.AbstractSingleColumnStandardBasicType;
导入org.hibernate.type.TypeResolver;
导入org.hibernate.usertype.ParameterizedType;
导入org.hibernate.usertype.usertype;
公共类GenericEnumUserType实现UserType、ParameterizedType{
私有类标识类型;
私有方法识别方法;
方法的私有方法价值;
私有静态最终字符串defaultIdentifierMethodName=“name”;
私有静态最终字符串defaultValueOfMethodName=“valueOf”;
私有抽象SingleColumnStandardBasicType类型;
私有int[]sqlTypes;
@凌驾
公共void setParameterValues(属性参数)
{
字符串enumClassName=parameters.getProperty(“enumClass”);
尝试
{
enumClass=Class.forName(enumClassName).asSubclass(Enum.Class);
}
捕获(ClassNotFoundException异常){
抛出新的HibernateeException(“未找到枚举类”,异常);
}
字符串identifierMethodName=parameters.getProperty(“identifierMethod”,defaultIdentifierMethodName);
尝试
{
identifierMethod=enumClass.getMethod(identifierMethodName,
新类[0]);
identifierType=identifierMethod.getReturnType();
}
捕获(异常)
{
抛出新的HibernateException(“未能选择标识符方法”,
例外情况);
}
TypeResolver tr=新的TypeResolver();
type=(AbstractSingleColumnStandardBasicType)tr.basic(identifierType.getName());
if(type==null)
{
抛出新的HibernateeException(“不支持的标识符类型”+identifierType.getName());
}
sqlTypes=newint[]{type.sqlType()};
String valueOfMethodName=parameters.getProperty(“valueOfMethod”,
方法名称的默认值);
尝试
{
valueOfMethod=enumClass.getMethod(valueOfMethodName,
新类[]{identifierType});
}
捕获(异常)
{
抛出新的HibernateException(“未能选择valueOf方法”,
例外情况);
}
}
@凌驾
公共类returnedClass()
{
返回枚举类;
}
@凌驾
公共对象nullSafeGet(结果集rs、字符串[]名称、对象所有者)
抛出HibernateeException、SQLException
{
对象标识符=type.get(rs,名称[0]);
尝试
{
返回值method.invoke(enumClass,新对象[]{identifier});
}
捕获(异常)
{
抛出新的HibernateeException(“调用枚举类的valueOfMethod时发生异常:”,
例外情况);
}
}
public void nullSafeSet(PreparedStatement st,Object value,int index)
抛出HibernateeException、SQLException
{
尝试
{
对象标识符=值!=null?identifierMethod.invoke(值,
新对象[0]):空;
st.setObject(索引、标识符);
}
捕获(异常)
{
抛出新的HibernateeException(“调用枚举类的identifierMethod时发生异常:”,
例外情况);
}
}
@凌驾
公共int[]sqlTypes()
{
返回sqlTypes;
}
@凌驾
公共对象汇编(可序列化缓存,对象所有者)
抛出冬眠异常
{
返回缓存;
}
@凌驾
公共对象深度复制(对象值)
抛出冬眠异常
{
返回值;
}
@凌驾
公共可序列化反汇编(对象值)
抛出冬眠异常
{
返回(可序列化)值;
}
@凌驾
公共布尔等于(对象x、对象y)
抛出冬眠异常
{
返回x==y;
}
@凌驾
公共整数哈希码(对象x)
抛出冬眠异常
{
返回x.hashCode();
}
公共布尔可交换()
{
返回false;
}
公共对象替换(对象原始、对象目标、对象所有者)
抛出冬眠异常
{
归还原件;
}
}
不幸的是,如果要基于枚举序号或名称进行序列化,则只能使用@Enumerated。有些情况下这是不合适的。我可以确认mtpettyp所说的。一种常见的情况是,用枚举替换“幻数”,但仍然必须保留原始数据。在我迁移到最新版本的hibernate之前,这种方法一直有效。自从5.3版本以来,新的TypeResolver()
已被弃用,我没有找到任何
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.TypeResolver;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
public class GenericEnumUserType implements UserType, ParameterizedType {
private Class<? extends Enum> enumClass;
private Class<?> identifierType;
private Method identifierMethod;
private Method valueOfMethod;
private static final String defaultIdentifierMethodName = "name";
private static final String defaultValueOfMethodName = "valueOf";
private AbstractSingleColumnStandardBasicType type;
private int[] sqlTypes;
@Override
public void setParameterValues( Properties parameters )
{
String enumClassName = parameters.getProperty("enumClass");
try
{
enumClass = Class.forName( enumClassName ).asSubclass( Enum.class );
}
catch (ClassNotFoundException exception) {
throw new HibernateException("Enum class not found", exception);
}
String identifierMethodName = parameters.getProperty( "identifierMethod", defaultIdentifierMethodName );
try
{
identifierMethod = enumClass.getMethod( identifierMethodName,
new Class[0]);
identifierType = identifierMethod.getReturnType();
}
catch (Exception exception)
{
throw new HibernateException("Failed to optain identifier method",
exception);
}
TypeResolver tr = new TypeResolver();
type = (AbstractSingleColumnStandardBasicType)tr.basic( identifierType.getName() );
if (type == null)
{
throw new HibernateException( "Unsupported identifier type " + identifierType.getName() );
}
sqlTypes = new int[] { type.sqlType() };
String valueOfMethodName = parameters.getProperty( "valueOfMethod",
defaultValueOfMethodName);
try
{
valueOfMethod = enumClass.getMethod( valueOfMethodName,
new Class[] { identifierType } );
}
catch ( Exception exception )
{
throw new HibernateException( "Failed to optain valueOf method",
exception);
}
}
@Override
public Class returnedClass()
{
return enumClass;
}
@Override
public Object nullSafeGet( ResultSet rs, String[] names, Object owner )
throws HibernateException, SQLException
{
Object identifier = type.get( rs, names[0] );
try
{
return valueOfMethod.invoke( enumClass, new Object[] { identifier } );
}
catch ( Exception exception )
{
throw new HibernateException( "Exception while invoking valueOfMethod of enumeration class: ",
exception);
}
}
public void nullSafeSet( PreparedStatement st, Object value, int index )
throws HibernateException, SQLException
{
try
{
Object identifier = value != null ? identifierMethod.invoke( value,
new Object[0] ) : null;
st.setObject( index, identifier );
}
catch ( Exception exception )
{
throw new HibernateException( "Exception while invoking identifierMethod of enumeration class: ",
exception );
}
}
@Override
public int[] sqlTypes()
{
return sqlTypes;
}
@Override
public Object assemble( Serializable cached, Object owner )
throws HibernateException
{
return cached;
}
@Override
public Object deepCopy( Object value )
throws HibernateException
{
return value;
}
@Override
public Serializable disassemble( Object value )
throws HibernateException
{
return (Serializable) value;
}
@Override
public boolean equals( Object x, Object y )
throws HibernateException
{
return x == y;
}
@Override
public int hashCode( Object x )
throws HibernateException
{
return x.hashCode();
}
public boolean isMutable()
{
return false;
}
public Object replace( Object original, Object target, Object owner )
throws HibernateException
{
return original;
}
}