PostGIS Geometry保存:“遇到无效的字节序标志值”

【字号: 日期:2024-04-01浏览:49作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决PostGIS Geometry保存:“遇到无效的字节序标志值”?

解决方案似乎如下: @Column使用JPA批注将字段映射到所需的列,@Type以使用方言指定Hibernate映射。

@Column(columnDeFinition = 'Geometry', nullable = true) @Type(type = 'org.hibernate.spatial.GeometryType')public Point centerPoint;

您可以在hibernate.cfg.xml文件中添加Hibernate属性,以查看db请求,并尝试使用基于文本的编辑器(例如Notepad ++,带有“UTF-8” /“ ANSI” /“其他字符集”)捕获字符串编码的问题。

<!--hibernate.cfg.xml --><property name='show_sql'>true</property><property name='format_sql'>true</property><property name='use_sql_comments'>true</property>

要添加hibernate属性,您将拥有一个hibernate.cfg.xml文件,其中包含以下内容。不要复制/粘贴它,因为它是面向MysqL的。只需看看我在前面插入的属性的位置即可。

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC '-//Hibernate/Hibernate Configuration DTD 3.0//EN' 'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'> <hibernate-configuration> <session-factory> <property name='hibernate.bytecode.use_reflection_optimizer'>true</property> <property name='hibernate.connection.driver_class'>com.MysqL.jdbc.Driver</property> <property name='hibernate.connection.password'>db-password</property> <property name='hibernate.connection.url'>jdbc:MysqL://localhost:3306/db-name</property> <property name='hibernate.connection.username'>db-username</property> <property name='hibernate.default_entity_mode'>pojo</property> <property name='hibernate.dialect'>org.hibernate.dialect.MysqL5InnoDBDialect</property> <property name='hibernate.format_sql'>true</property> <property name='hibernate.search.autoregister_listeners'>false</property> **<property name='hibernate.show_sql'>true</property>** <property name='hibernate.use_sql_comments'>false</property> <mapping ressource='....' /> <!-- other hbm.xml mappings below... --> </session-factory> </hibernate-configuration>

记录所有sql的另一种方法是在log4j.properties文件内添加特定于包的属性:

log4j.logger.org.hibernate.sql=DEBUGlog4j.logger.org.hibernate.type=TRACE

祝好运!

解决方法

我有一个Spring Roo + Hibernate项目,该项目需要从客户端应用程序输入JTS知名文本(WKT)字符串,然后将其转换为JTSGeometry对象,然后尝试将其写入PostGIS数据库。我在JDBC连接和类型方面遇到了一些问题,但这些问题似乎已通过以下方式解决:

@Column(columnDefinition = 'Geometry',nullable = true) private Geometry centerPoint;

转换完成:

Geometry geom = new WKTReader(new GeometryFactory(new PrecisionModel(),4326)).read(source);

但是现在,当Hibernate尝试将我的Geometry对象写入数据库时​​,我得到一个错误:

2012-08-31 21:44:14,096 [tomcat-http--18] ERROR org.hibernate.util.JDBCExceptionReporter - Batch entry 0 insert into land_use (center_point,version,id) values (’<stream of 1152 bytes>’,’0’,’1’) was aborted. Call getNextException to see the cause.2012-08-31 21:44:14,096 [tomcat-http--18] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: Invalid endian flag value encountered.

似乎很明显,该错误与二进制表示形式有关,该表示形式大概是作为具有某些字节序的众所周知的二进制(WKB)生成的。但是,由于Hibernate隐藏了所有持久性,所以我无法真正确定事情的发展方向。

我已经为这个“几何”问题战斗了好几天了,关于这些错误的信息很少,所以有人有什么好主意吗?我可以在某个地方(Hibernate或PostGIS)指定字节顺序,还是以其他格式(WKT)存储?

编辑: 我还应该提到我正在使用最新的一切,通常似乎是兼容的:

Spring 3.1.1,Roo 1.2.1hibernate3.6.9hibernate空间4.0-M1捷报1.12PostgreSQL 9.1postgis-jdbc 1.5.3(不是最新版本,但建议用于hibernate-spatial,从源代码编译)postgis-jdbc 2.0.1(现在尝试此操作以匹配与PostgreSQL安装的版本,相同的问题)

在Hibernate的空间4教程建议我做的属性注解为:

@Type(type='org.hibernate.spatial.GeometryType')private Geometry centerPoint;

…但是当我这样做时,我得到了另一个错误,当前注释可以解决。

相关文章: