2016-11-13 60 views
1

這裏對Hibernate來說相當新穎。我試圖從MYSQL數據庫中檢索對象列表,其中一列是BLOB。Java休眠異常:無法反序列化

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$2.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$$913d8323.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$$8a257e3c.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$2.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$$913d8323.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$$8a257e3c.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) 

注意:我知道註釋的可用文檔多得多,但這是一些我必須簡單維護的遺留代碼,升級它不取決於我。我真的很感激任何幫助。提前致謝!

回答

0

答案就像打字錯誤一樣簡單。我之前已經映射了一個子類,最後我不需要這個子類,而且我忘記刪除結束標記。 哎喲,休眠異常不是自解釋的嗎?感謝Matteo的輸入!

1

嘗試用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);