gpt4 book ai didi

spring - 使用 postgis 和 Spring hibernate 空间 : can't get back spatial data

转载 作者:行者123 更新时间:2023-11-29 12:33:50 26 4
gpt4 key购买 nike

我有这个问题:我可以写入带有地理类型列的 Postgis 表,并且数据输入正确。当我试图取回它时,我得到一个 java.lang.IllegalArgumentException: Can't convert object of type org.postgresql.util.PGobject

我正在使用 Hibernate Spatial 4.0M1、Postgis 2.0.2、Postgresql 9.1.7 和 Spring 3.2、Postgis-jdbc 2.0.2,我还尝试了 1.5.2 和 Postgresql-jdbc 9.1-901。

这是我类(class)的相关部分

public class Poi {
@Id
@GeneratedValue(generator="t_pois_poisid_seq")
private long poisid;
@Column(name="name")
private String name;
@Column(name="userid")
private int owner;
//@Column(columnDefinition = "Geometry", name="location")
@Type(type="org.hibernate.spatial.GeometryType")
private Point location;


public Poi(){}

public Poi(String name, int owner, String wktPoint){
this.name = name;
this.owner = owner;
WKTReader fromText = new WKTReader();
Geometry geom = null;
try{
geom = fromText.read(wktPoint);
}catch(ParseException e){
throw new RuntimeException("Not a WKT string:" + wktPoint);
}
if (!geom.getGeometryType().equals("Point")) {
throw new RuntimeException("Geometry must be a point. Got a " + geom.getGeometryType());
}
//geom.setSRID(4326);
this.location = (Point) geom;

}

这是我得到的错误

java.lang.IllegalArgumentException: Can't convert object of type org.postgresql.util.PGobject org.hibernate.spatial.dialect.postgis.PGGeometryValueExtractor.toJTS(PGGeometryValueExtractor.java:99) org.hibernate.spatial.dialect.AbstractJTSGeometryValueExtractor.extract(AbstractJTSGeometryValueExtractor.java:42) org.hibernate.spatial.dialect.AbstractJTSGeometryValueExtractor.extract(AbstractJTSGeometryValueExtractor.java:37) 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:2807) org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1545) org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1477) org.hibernate.loader.Loader.getRow(Loader.java:1377) org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:644) org.hibernate.loader.Loader.doQuery(Loader.java:854) org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293) org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:263) org.hibernate.loader.Loader.loadEntity(Loader.java:1977) org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82) org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72) org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3821) org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:458) org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:427) org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204) org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260) org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148) org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1075) org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:175) org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2421) org.hibernate.internal.SessionImpl.get(SessionImpl.java:971) com.prismio.pois.api.repository.HibernatePoiRepository.findOne(HibernatePoiRepository.java:30) 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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 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.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) $Proxy24.findOne(Unknown Source) com.prismio.pois.api.controller.PoiController.getUser(PoiController.java:40) 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.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 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)

最佳答案

如果有人感兴趣:问题是 PostGIS 从列类型 Geography 而不是 PGGeometry 返回一个 PGObject,因此转换为PGgeometry 是不可能的。我发现使用 jdbcTemplate(而不是 Hibernate)的解决方案是将字段从结果集中转换为 PGObject,然后像这样创建一个 PGGeometry:

PGgeometry geo = new PGgeometry(myPGObject.getValue());

执行此操作后,geo.getValue() 返回一个字符串,格式为 SRID=4326;POINT(40.70686417491799 -74.01572942733765),这正是我想要的。我可以使用 ST_AsText 进行查询,但它会失去精度。我不太了解 Hibernate,无法提出解决方案,但这对我的用例来说已经足够了。

关于spring - 使用 postgis 和 Spring hibernate 空间 : can't get back spatial data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14246729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com