关于hibernate查询映射时无法反序列化问题

org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize

以上是报错信息,网上查到的是说字段类型没有设置对应的类型,或者类型设置错误,这里根据我自己的实际情况,做一下补充情况

当在xml文件中设置字段的类型为枚举类型时,不能直接设置为枚举对象类,比如:

这样就不对了,无法解析,枚举类型应该要通过枚举类来解析,如下:

EnumUserType类如下:

package com.**.**.dao.usertype;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class EnumUserType<E extends Enum<E>> implements UserType {
	private Class<E> clacz	= null;

	protected EnumUserType(Class<E> c) {
		this.clacz = c;
	}

	private static final int[]	SQL_TYPES	= { Types.VARCHAR };

	public int[] sqlTypes() {
		return SQL_TYPES;
	}

	public Class<E> returnedClass() {
		return clacz;
	}

	public Object nullSafeGet( ResultSet resultSet, String[] names, Object owner )
			throws HibernateException, SQLException {
		String name = resultSet.getString( names[0] );
		E result = null;
		if ( !resultSet.wasNull() ) {
			result = Enum.valueOf( clacz, name );
		}
		return result;
	}

	public void nullSafeSet( PreparedStatement preparedStatement, Object value,
			int index ) throws HibernateException, SQLException {
		if ( null == value ) {
			preparedStatement.setNull( index, Types.VARCHAR );
		} else {
			preparedStatement.setString( index, ((Enum<?>) value).name() );
		}
	}

	public Object deepCopy( Object value ) throws HibernateException {
		return value;
	}

	public boolean isMutable() {
		return false;
	}

	public Object assemble( Serializable cached, Object owner )
			throws HibernateException {
		return cached;
	}

	public Serializable disassemble( Object value ) throws HibernateException {
		return (Serializable) value;
	}

	public Object replace( Object original, Object target, Object owner )
			throws HibernateException {
		return original;
	}

	public int hashCode( Object x ) throws HibernateException {
		return x.hashCode();
	}

	public boolean equals( Object x, Object y ) throws HibernateException {
		if ( x == y )
			return true;
		if ( null == x || null == y )
			return false;
		return x.equals( y );
	}
}

  

以上仅是这类错误的一种原因,记录一下

 

posted on 2018-06-01 15:13  binTke  阅读(926)  评论(0编辑  收藏  举报

导航