Java Hibernate 异常:无法反序列化

Java Hibernate Exception: could not deserialize

这里是 Hibernate 的新手。我正在尝试从 MYSQL 数据库中检索对象列表,其中一列是 BLOB。

Item.java

public class Item implements Serializable{

    private static final long serialVersionUID = -7040387542178744607L;
    private int id;
    private String name;     
    private User owner;
    private int rating;
    private String review;
    private byte[] coverImage;  //getters, setters, etc.

}

ItemDao.java

public List<Item> getAllItems() {
        Session s = sessionFactory.getCurrentSession();
        Query q = s.createQuery("from Item");
        List<Item> items = new ArrayList<Item>();

        items = q.list();

        return items;
    }

Item.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="myApp.Item" table="ITEM" >     
        <id name="id" column="ITEM_ID" >
            <generator class="native" />
        </id>
        <many-to-one name="owner" class="myApp.User"
            column="OWNER_ID" unique="true" not-null="true"
            cascade="all" />
        <property name="name" column="NAME" ></property>
        <property name="rating" column="RATING" ></property>
        <property name="review" column="REVIEW" ></property>
        <property name="coverImage" column="COVER_IMAGE"></property>
        </subclass>
    </class>
</hibernate-mapping>

堆栈跟踪:

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.type.SerializationException: could not deserialize
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.hibernate.type.SerializationException: could not deserialize
    org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:262)
    org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:306)
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:131)
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:117)
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:39)
    org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor.doExtract(VarbinaryTypeDescriptor.java:67)
    org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:65)
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:269)
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:265)
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
    org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:357)
    org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2705)
    org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1544)
    org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1476)
    org.hibernate.loader.Loader.getRow(Loader.java:1376)
    org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:643)
    org.hibernate.loader.Loader.doQuery(Loader.java:853)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:292)
    org.hibernate.loader.Loader.doList(Loader.java:2381)
    org.hibernate.loader.Loader.doList(Loader.java:2367)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2197)
    org.hibernate.loader.Loader.list(Loader.java:2192)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1248)
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    ar.edu.uces.progweb2.merlo2.dao.ItemDao.getAllItems(ItemDao.java:42)
    ar.edu.uces.progweb2.merlo2.dao.ItemDao$$FastClassByCGLIB$3d8323.invoke(<generated>)
    net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
    org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    ar.edu.uces.progweb2.merlo2.dao.ItemDao$$EnhancerByCGLIB$a257e3c.getAllItems(<generated>)
    ar.edu.uces.progweb2.merlo2.controller.persistence.ItemController.list(ItemController.java:58)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.io.EOFException
    java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2353)
    java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2822)
    java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
    java.io.ObjectInputStream.<init>(ObjectInputStream.java:301)
    org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:328)
    org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:318)
    org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:237)
    org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:306)
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:131)
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:117)
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:39)
    org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor.doExtract(VarbinaryTypeDescriptor.java:67)
    org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:65)
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:269)
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:265)
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
    org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:357)
    org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2705)
    org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1544)
    org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1476)
    org.hibernate.loader.Loader.getRow(Loader.java:1376)
    org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:643)
    org.hibernate.loader.Loader.doQuery(Loader.java:853)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:292)
    org.hibernate.loader.Loader.doList(Loader.java:2381)
    org.hibernate.loader.Loader.doList(Loader.java:2367)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2197)
    org.hibernate.loader.Loader.list(Loader.java:2192)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1248)
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
    ar.edu.uces.progweb2.merlo2.dao.ItemDao.getAllItems(ItemDao.java:42)
    ar.edu.uces.progweb2.merlo2.dao.ItemDao$$FastClassByCGLIB$3d8323.invoke(<generated>)
    net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
    org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    ar.edu.uces.progweb2.merlo2.dao.ItemDao$$EnhancerByCGLIB$a257e3c.getAllItems(<generated>)
    ar.edu.uces.progweb2.merlo2.controller.persistence.ItemController.list(ItemController.java:58)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

旁注:我确实知道注释有更多可用的文档,但这是我必须简要维护的一些遗留代码,我不能升级它。我真的很感激这方面的任何帮助。提前致谢!

尝试使用 java.sql.Blob 代替:

public class Item implements Serializable{
    private static final long serialVersionUID = -7040387542178744607L;
    private int id;
    private String name;     
    private User owner;
    private int rating;
    private String review;
    private Blob coverImage;  //getters, setters, etc.
}

您的实体映射:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="myApp.Item" table="ITEM" >     
        <id name="id" column="ITEM_ID" >
            <generator class="native" />
        </id>
        <many-to-one name="owner" class="myApp.User"
            column="OWNER_ID" unique="true" not-null="true"
            cascade="all" />
        <property name="name" column="NAME" ></property>
        <property name="rating" column="RATING" ></property>
        <property name="review" column="REVIEW" ></property>
        <property name="coverImage" column="COVER_IMAGE" type="blob"></property>
    </class>
</hibernate-mapping>

然后您可以使用它来获取您的字节数组(如果您需要这种格式的 属性):

int blobLength = (int) blob.length(); 
byte[] coverImageAsBytes = coverImage.getBytes(1, blobLength);

答案很简单,就是打错了字。我之前已经映射了一个子类,但我最终不需要它,而且我忘记删除结束标记。 哎呀,休眠异常不是不言自明的吗?感谢 Matteo 的输入!