小编典典

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

hibernate

我有一个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.1
  • hibernate3.6.9
  • hibernate空间4.0-M1
  • 捷报1.12
  • PostgreSQL 9.1
  • postgis-jdbc 1.5.3(不是最新版本,但建议用于hibernate-spatial,从源代码编译)
  • postgis-jdbc 2.0.1(现在尝试此操作以匹配与PostgreSQL安装的版本,相同的问题)

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

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

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


阅读 364

收藏
2020-06-20

共1个答案

小编典典

解决方案似乎如下: @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=DEBUG
log4j.logger.org.hibernate.type=TRACE

祝好运!

2020-06-20