2013-11-03 159 views
1

在我的數據庫我有這個表:PGpoint休眠(反)序列化異常

CREATE TABLE base_station 
(
    MCC INT NOT NULL, 
    MNC INT NOT NULL, 
    LAC INT NOT NULL, 
    CID INT NOT NULL, 
    type TEXT NOT NULL, 
    geoposition point NOT NULL, 
    city TEXT, 
    date_created DATE, 
    PRIMARY KEY (CID, MNC, LAC, MCC), 
    FOREIGN KEY (MNC, LAC, MCC) REFERENCES location_area (MNC, LAC, MCC) ON DELETE CASCADE DEFERRABLE 
); 

其中geoposition是指向型的。我讀過的PostgreSQL點應被映射到PGpoint,而是試圖做反序列化數據時,我得到這個異常:

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:140) 
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:121) 
    org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:44) 
    org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:67) 
    org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64) 
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:261) 
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:257) 
    org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:247) 
    org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:332) 
    org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2912) 
    org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1673) 
    org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1605) 
    org.hibernate.loader.Loader.getRow(Loader.java:1505) 
    org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:713) 
    org.hibernate.loader.Loader.processResultSet(Loader.java:943) 
    org.hibernate.loader.Loader.doQuery(Loader.java:911) 
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342) 
    org.hibernate.loader.Loader.doList(Loader.java:2526) 
    org.hibernate.loader.Loader.doList(Loader.java:2512) 
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342) 
    org.hibernate.loader.Loader.list(Loader.java:2337) 
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495) 
    org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357) 
    org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195) 
    org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269) 
    org.hibernate.internal.QueryImpl.list(QueryImpl.java:101) 
    pl.elka.networkwatcher.dao.implemented.HibernateDAOImpl.getList(HibernateDAOImpl.java:49) 
    pl.elka.networkwatcher.managers.BaseStationManager.findAll(BaseStationManager.java:25) 
    pl.elka.networkwatcher.managers.BaseStationManager$$FastClassByCGLIB$$bbbd79f1.invoke(<generated>) 
    org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 
    org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698) 
    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.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) 
    pl.elka.networkwatcher.managers.BaseStationManager$$EnhancerByCGLIB$$30a97444.findAll(<generated>) 
    pl.elka.networkwatcher.controllers.MainController.stations(MainController.java:61) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    java.lang.reflect.Method.invoke(Method.java:601) 
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:439) 
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:427) 
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) 
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 

當我從Hibernate映射刪除geoposition,它工作正常。 這裏是我的一塊實體類:

@Column(name = "geoposition", nullable = false) 
private PGpoint geoposition; 

我使用PostgreSQL 9.1沒有PostGIS的。 我該如何解決這個問題,讓休眠工作正常?

+0

有沒有使用PostGIS的具體原因是什麼? – TheSteve0

+1

我的項目中不需要PostGIS。唯一的需要是具有單個屬性地理位置而不是兩個:緯度和經度。但似乎不可能這樣做.. – mattis

+0

我也有這個問題。我正在使用hibernate空間。 – Zerkz

回答

2

我假設你使用的是PostgreSQL的point類型?如果是這樣的話,它看起來像Hibernate不明白這種類型,或者它沒有在JDBC驅動程序中定義。

我在使用Hibernate Spatial時遇到了類似的問題,我沒有找到postgis.jar。

我會開始可能通過嘗試直接JDBC來使用PostgreSQL point類型。如果這可以工作,然後看看休眠。

1

一兩件事你可以做的是,如果你不需要對數據庫列的點類型,您可以更改到BYTEA

如:

@Column(name = "geoposition", columnDefinition = "bytea") 
public PGpoint getGeoposition() 
{ 
    return this.geoposition; 
} 

一個更好的辦法是設立一個用戶類型,在這裏你可以找到一個很好的例子: http://learningviacode.blogspot.de/2011/09/creating-hibernate-custom-type-1.html

這裏是我做過什麼:

package testapplication; 

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; 
import org.postgresql.geometric.PGpoint; 


public class PointUserType implements UserType 
{ 

    @Override 
    public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) 
     throws HibernateException, SQLException 
    { 
    assert names.length == 1; 
    if (resultSet.wasNull()) 
    { 
     return null; 
    } 
     final PGpoint point = new PGpoint(resultSet.getObject(names[0]).toString()); 
     return point; 
    } 

    @Override 
    public void nullSafeSet(PreparedStatement statement, Object value, int index) 
     throws HibernateException, SQLException 
    { 
     statement.setObject(index, value); 
    } 

    @Override 
    public int[] sqlTypes() 
    { 
     return new int[] 
     { 
      Types.VARCHAR 
     }; 
    } 

    @SuppressWarnings("rawtypes") 
    @Override 
    public Class returnedClass() 
    { 
     return PGpoint.class; 
    } 

    @Override 
    public boolean equals(Object o, Object o1) throws HibernateException 
    { 
     boolean isEqual = false; 
     if (o == o1) 
     { 
       isEqual = true; 
     } 
     if (o == null || o1 == null) 
     { 
      isEqual = false; 
     } 
     else 
     { 
      isEqual = o.equals(o1); 
     } 
     return isEqual; 
    } 

    @Override 
    public int hashCode(Object o) throws HibernateException 
    { 
     return o.hashCode(); 
    } 

    @Override 
    public Object deepCopy(Object o) throws HibernateException 
    { 
     return (Serializable) o; 
    } 

    @Override 
    public boolean isMutable() 
    { 
     return true; 
    } 

    @Override 
    public Serializable disassemble(Object o) throws HibernateException 
    { 
     return (Serializable) o; 
    } 

    @Override 
    public Object assemble(Serializable srlzbl, Object o) throws HibernateException 
    { 
     return srlzbl; 
    } 

    @Override 
    public Object replace(Object o, Object o1, Object o2) throws HibernateException 
    { 
     return this; 
    } 

} 

你列定義應該是這樣的:

@Column(name = "geoposition", columnDefinition = "point") 
@Type(type = "testapplication.PointUserType") 
public PGpoint getGeoposition() 
{ 
    return geoposition; 
} 

這允許你保存和PostgreSQL的點列讀取。我沒有一直在測試它,但它應該讓你開始。

0

也許你仍然需要方言設置爲PostGIS的:hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect