Hibernate Spatial 5 - GeometryType

后端 未结 6 1880
刺人心
刺人心 2021-02-01 16:36

After upgrading Hibernate-spatial to Version 5.0.0.CR2 the following declaration doesn\'t work anymore:

@Column(columnDefinition = \"geometry(Point,4326)\")
@Typ         


        
相关标签:
6条回答
  • 2021-02-01 16:54

    For Hibernate Spatial 5.2.x, all you need is the below in your entity.

    private Point location;

    You don't need columnDefinition or Type like the above solutions mentioned.

    Some additional details and checks to see if the above works

    • As soon as you run your setup and use desc table_namein mysql, you should see the field type of geometry. This indicates that your hibernate to database mapping is working fine.
    • Now try to create an entity and repo.save(entity) from java and your entity should save fine without any errors.

    If the above setup didn't work well, you will typically get the error

    Data truncation: Cannot get geometry object from data you send to the GEOMETRY field Blockquote

    Hope that helps someone save a 6 hours that I wasted !

    0 讨论(0)
  • 2021-02-01 16:55

    Well the solution is too easy to see. Simply delete the @Type annotation, so the declaration looks like this:

    @Column(columnDefinition = "geometry(Point,4326)")
    private Point position;
    

    Source:

    Note the @Type annotation. This informs Hibernate that the location attribute is of type Geometry. The @Type annotation is Hibernate specific, and the only non-JPA annotation that is required. (In future versions of Hibernate (version 5 and later) it will no longer be necessary to explicitly declare the Type of Geometry-valued attributes.)

    0 讨论(0)
  • 2021-02-01 16:55

    I am using hibernate 4.3.6 and hibernate spatial 5.2.2. The solution suggested above didn't work for me. However the below code worked If I add hibernate spatial 4.3

    @Type(type="org.hibernate.spatial.GeometryType")
    private Point location;
    
    0 讨论(0)
  • 2021-02-01 17:01

    Apparently your Hibernate libraries need to be at the same version too.

    I was using postgis / springboot / hibernate-spatial.

    Initially Hibernate-Spatial was @ 5.4.10.Final, and didn't work even with the solutions here.

    I then looked at what version of hibernate was in my maven dependencies ( hibernate-commons-annotations-5.1.0.Final.jar) and remembered seeing somewhere that the versions of hibernate need to match.

    So I downgraded my Hibernate-Spatial, to 5.1 and the following mapping statement works without any more info:

    // Geometry Object is from the following import
    import com.vividsolutions.jts.geom.*;
    
    @Column(name="domain_loc")
    private Geometry location;
    
    0 讨论(0)
  • 2021-02-01 17:03

    The solution suggested by Denis above did not work for me on Hiberate 5 spatial and Mysql. However the following annotations worked for me

      @Column(name = "location",columnDefinition="Geometry")
      private Geometry location;
    
    
      @Column(name = "pointlocation",columnDefinition="Point")
      private Point pointlocation;
    

    If you are running Hibernate 5.3+ you can skip out on column definitions

      @Column(name = "location")
      private Geometry location;
    
    
      @Column(name = "pointlocation")
      private Point pointlocation;
    
    0 讨论(0)
  • 2021-02-01 17:17

    So I've been battling this problem for a few days, the thing is that I wanted the database type to be from the postgis type in order for me to run efficient distance queries for nearest neighbour etc. I finally figured out how to do it and wanted to share it with the community so I created a demo project containing the solution to the problem.

    My setup is a PostgreSQL database with Postgis, Spring boot jpa, Hibernate 5+, Hibernate-spatial 5+ and also added converting to rest output, which again required a special module from jackson.

    the project is found at https://github.com/Wisienkas/springJpaGeo

    the important code you was asking for was this:

    @type(type = "jts_geometry")
    private Point point;
    
    0 讨论(0)
提交回复
热议问题