Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data JPA and Geometry type

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

like image 585
Ido Barash Avatar asked Aug 25 '16 13:08

Ido Barash


People also ask

What is the difference between spring JPA and Spring Data JPA?

Spring data jpa- it is same like jpa means we can describe in below way. Spring Data Jpa is jpa data abstraction access which means it likes a jpa but it add some extra functionality, Without jpa we can not implement the spring data jpa.

What is difference between JpaRepository and PagingAndSortingRepository?

PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

Should I use Spring Data JPA or JDBC?

Spring Data JDBC has less abstractions than Spring Data JPA, but uses Spring Data concepts to make it easier to do CRUD operations than Spring JDBC. It sits closer to the database because it does not contain the most part of the Spring Data magic when querying the database.

What is Spring Data JPA?

Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that's actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.


1 Answers

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.

like image 171
benizra Avatar answered Nov 23 '22 14:11

benizra