Spring data JPA: cannot persist object with attribute of type Point

Viewed 40

While trying to persist an object with an attribute of type Point, defined as follows:

@Entity
@Table(name = "event")
public class Event {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(columnDefinition = "POINT")
    private Point coordinate;

}

I'm getting the error Data truncation: Cannot get geometry object from data you send to the GEOMETRY field.

The corresponding database table is defined as follows:

create table event
(
   id bigint not null auto_increment,
   coordinate POINT,
   primary key (id)
)
engine= InnoDB;

The application uses Spring Boot 2.7.3 and talks to a MySQL Server 8.0. In the application.properties the database is configured as follows:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=UTF-8&allowPublicKeyRetrieval=true
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL8SpatialDialect

And I've added the following dependency to my pom.xml:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-spatial</artifactId>
</dependency>

Here's the full log:

2022-09-24 11:21:41.386 DEBUG 18820 --- [nio-8080-exec-1] org.hibernate.SQL                        : insert into event (coordinate) values (?)
2022-09-24 11:21:41.395 TRACE 18820 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARBINARY] - [Point [x=48.164455, y=7.544595]]
2022-09-24 11:21:41.427  WARN 18820 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1416, SQLState: 22001
2022-09-24 11:21:41.427 ERROR 18820 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Data truncation: Cannot get geometry object from data you send to the GEOMETRY field
2022-09-24 11:21:41.508 DEBUG 18820 --- [nio-8080-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
2022-09-24 11:21:41.572 ERROR 18820 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement] with root cause

What am I missing?

Edit 1: the following SQL statement works fine in MySQL Workbench: INSERT INTO event (coordinate) VALUES (POINT(48.164455, 7.544595));

0 Answers
Related