PostGIS Geometry saving: “Invalid endian flag value encountered.”

后端 未结 5 1706
广开言路
广开言路 2020-12-07 01:05

I have a Spring Roo + Hibernate project which takes a JTS well-known text (WKT) String input from the client application, converts it into a JTS Geometry object, and then at

相关标签:
5条回答
  • 2020-12-07 01:18

    pilladooo's solution works with spring boot 2.0.3, hibernate/spatial 5.2.17.Final, Postgres 9.5.

    Column in entity in my case is defined as @Column(name = "geometry") private Geometry geometry;

    and in database as type 'geometry' (to avoid bytea type that hibernate auto generates)

    Firstly I solved 'Invalid endian flag value encountered' with adding columnDefinition = "geometry", but after that hibernate would fail schema validation with "Schema-validation: wrong column type encountered in column [geometry] in table [my_shema.my_geometry_table]; found [geometry (Types#OTHER)], but expecting [bytea (Types#VARBINARY)]"

    after adding spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect it finally worked. ColumnDefinition is also redundant now

    0 讨论(0)
  • 2020-12-07 01:21

    I solve this problem adding to 'application.properties' this line:

    spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
    
    0 讨论(0)
  • 2020-12-07 01:29

    See also, http://trac.osgeo.org/postgis/ticket/1830 An issue arose around the time of Postgresql 9xx and Postgis 2xx coming out which caused the same "an invalid endian flag" error when using the postgres utility pgsql2shp. It can be fixed be removing old versions of the library libpq.so, as it was due to Postgres changing default behavior of bytea.

    0 讨论(0)
  • 2020-12-07 01:33

    After some fight with given problem here are steps that helped me to solve it. First I should mention I'm using WildFly 17 server, PostgreSQL 12 and PostGIS 3.0.0. And now steps that I think are important in this problem:

    Make jboss-deployment-structure.xml file in META-INF (if you don't have one), and exclude hibernate that comes with WildFly

    <jboss-deployment-structure>
        <deployment>
            <exclusions>
                <module name="org.hibernate" />
            </exclusions>
        </deployment>
    </jboss-deployment-structure>
    

    In your pom.xml add dependencies

    <dependency>
        <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        <version>5.4.12.Final</version>
    </dependency>
    

    and

    <dependency>
        <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
        <version>5.4.12.Final</version>
    </dependency>
    

    Make sure that hibernate core and hibernate spatial have same version (use whatever version you like).

    Your persistence.xml should have property

    <property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
    

    And finally, I don't think this is very important but I used org.locationtech.jts for geometry in Java.

    I hope I didn't skip anything important and that these are required steps. It is possible that there is something else, but many hours have passed in trying different solutions and it is possible that I forgot to include some dependency/property. Answer is based on personal experience, therefore feel free to comment, to prove me wrong or to expand answer. Anyhow I hope someone will find this answer useful.

    0 讨论(0)
  • 2020-12-07 01:39

    The solution seems to be the following:
    @Column to map the field to the desired column with JPA annotations
    @Type to specify the Hibernate mapping with the dialect.

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

    You could add the Hibernate property inside the hibernate.cfg.xml file to see the db request and try to catch the string-encoded problem with a text based editor like Notepad++ with "UTF-8"/"ANSI"/"other charsets"

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

    To add the hibernate properties you will have an hibernate.cfg.xml file with the following stuff. Don't copy/paste it because it is MySQL oriented. Just look where I have inserted the properties I evocated previously.

     <?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>
    

    Another way to log all sql is to add package specific properties inside a log4j.properties file:

    log4j.logger.org.hibernate.SQL=DEBUG
    log4j.logger.org.hibernate.type=TRACE
    

    Good luck!

    0 讨论(0)
提交回复
热议问题