spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.03k stars 1.43k forks source link

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

Closed olofumark closed 1 day ago

olofumark commented 2 days ago

Someone please help. I'm trying to insert a simple point into MySQL but have been quite unsuccessful due to an error.

My Entity class

@Entity(name = "listing")
@Data
public class Listing implements Serializable { 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "location" , columnDefinition = "POINT")
    private Point location;
}   

This is how I prepare a point for insertion:

public static Point getPointFromXY(double longitude, double latitude) {
    GeometryFactory geometryFactory = new GeometryFactory();
    return geometryFactory.createPoint(new Coordinate(longitude, latitude));
}

My Repository class

public interface ListingRepository extends CrudRepository<Listing, Long> {
}

And here is the Controller portion

Listing listing = listingMapper.toListing(listingDTO);
llisting.setLocation(SharedUtils.getPointFromXY(listingDTO.getLongitude(), listingDTO.getLatitude()));
Listing resultListing = listingRepository.save(listing);

Here's the error stacktrace

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:104) ~[mysql-connector-j-8.0.32.jar:8.0.32] at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916) ~[mysql-connector-j-8.0.32.jar:8.0.32] at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1061) ~[mysql-connector-j-8.0.32.jar:8.0.32] at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1009) ~[mysql-connector-j-8.0.32.jar:8.0.32] at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1320) ~[mysql-connector-j-8.0.32.jar:8.0.32] at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:994) ~[mysql-connector-j-8.0.32.jar:8.0.32] at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-5.0.1.jar:na] at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-5.0.1.jar:na] at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:194) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final] at org.hibernate.id.insert.GetGeneratedKeysDelegate.performInsert(GetGeneratedKeysDelegate.java:107) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final] at org.hibernate.engine.jdbc.mutation.internal.MutationExecutorPostInsertSingleTable.execute(MutationExecutorPostInsertSingleTable.java:100) ~[hibernate-core-6.4.1.Final.jar:6.4.1.Final]

What happens? The entity table gets created fine and I can manually insert points into the table but gives error via code.

Thanks for any insights

mp911de commented 1 day ago

This is an issue between your model and your database. Not sure how much Hibernate is involved, in any case that's not a Spring Data issue.

olofumark commented 1 day ago

@mp911de Thanks for the response but let me add something, When I manually insert Points into the table, reading them back via Spring Data also throws a corrupted a corrupted stream error:: java.io.StreamCorruptedException: invalid stream header: 00000000 at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:958) ~[na:na] at java.base/java.io.ObjectInputStream.(ObjectInputStream.java:392) ~[na:na]

And looking at the simplicity of my model, I'm at a loss regarding what to tweak. Kindly help me understand what might be wrong between my model and database or what to do please.