7

I am developing an application that will run on both MySql and MS SQL.

I have a field that is "geometry" type for spatial.

By using:

 @Column(columnDefinition = "geometry")
 private Point geometry;

(point is org.springframework.data.geo.Point)

Hibernate creates the field properly (hbm2ddl).

But inserting any point does not work. I get : Data truncation: Cannot get geometry object from data you send to the GEOMETRY field

I use spring-boot-jpa-starter.. and not direct hibernate.

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
       <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>5.2.2.Final</version>
        </dependency>

Regards, Ido

Ido Barash
  • 4,856
  • 11
  • 41
  • 78
  • Have you specified a `Hibernate Spatial dialect`. See http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#spatial – Anton N Aug 25 '16 at 15:14
  • Yes i have. Still not working – Ido Barash Aug 25 '16 at 15:14
  • Can you share a `Spring Data Repository` class and a `Spring Boot` configuration properties/yml – Anton N Aug 25 '16 at 15:55
  • Probably your point should be from following package `com.vividsolutions.jts.geom.Point;` See: http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#spatial-types – Anton N Aug 25 '16 at 16:05

1 Answers1

19

Hello I have successfully mapped a point in JPA. Here's what I did:

  1. I have this on Maven:

    <dependency>
        <groupId>com.vividsolutions</groupId>
        <artifactId>jts</artifactId>
        <version>1.13</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>5.2.5.Final</version>
    </dependency>
    
  2. I have this on my entity:

    @Column(name = "locationpoint", columnDefinition = "POINT") 
    private Point locationpoint;
    
  3. I have this on my application.properties:

    # needed for Location domain class
    spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect
    
  4. I can get the value using this:

    locationRepository.findOne((long) 1).getLocationpoint().getX();
    locationRepository.findOne((long) 1).getLocationpoint().getY();
    

I based my solution from here Matti Tahvonen's example:

https://github.com/mstahv/spring-boot-spatial-example

Hope this helps.

benizra
  • 346
  • 3
  • 6
  • I face those issues and gathered thereafter my react springboot mini dem in https://github.com/hbadri1/React_Springboot_Mapbox/blob/master/README.md – Houssam Badri Oct 24 '19 at 11:57