javamysqlhibernatejts

Point data type into MySQL8


I have a class with JTS point:

@Entity
@Data
public class Check {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    private Long id;
    private Point location;

I then want to save that point to my database

   Point location = new GeometryFactory().createPoint((new Coordinate(10, 10, 4326)));
   // Hibernate method to save entity

After doing some reading I can see people using a WKTReader to convert to some other format (WKB?) before storing it in the MySQL db.

When I try the stack trace says :

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1406, SQLState: 22001
o.h.engine.jdbc.spi.SqlExceptionHelper   : Data truncation: Data too long for column 'location' at row 1

UPDATE

Using dependency org.geolatte.geom.* works perfectly:

        Point<G2D> geolatte_location = point(WGS84,g(1744.33,-53.21));

Which makes me think I need to convert the point before I store it - how do I convert it so it will be able to be used in the constructor and resulting SQL?

Environment: Java 18 Gradle MySQL8 Hibernate 6.2 Spring Boot 3.2

The column in the table for the point created by hibernate is varbinary(255) - I thought MySQL8 supported Spatial point type.


Solution

  • I did the test with Spring boot 3.1.2 and H2 database.

    Add the hibernate spatial dependency in your project(version may vary).

    // https://mvnrepository.com/artifact/org.hibernate/hibernate-spatial
    implementation 'org.hibernate:hibernate-spatial:6.2.6.Final'

    Then you will see that the column definition of location is geometry. No transformation needed.

    Hibernate spatial uses the locationtech library.