Hibernate Spatial 5 - GeometryType

For Hibernate Spatial 5.2.x, all you need is the below in your entity.

private Point location;

You don't need columnDefinition or Type like the above solutions mentioned.

Some additional details and checks to see if the above works

  • As soon as you run your setup and use desc table_namein mysql, you should see the field type of geometry. This indicates that your hibernate to database mapping is working fine.
  • Now try to create an entity and repo.save(entity) from java and your entity should save fine without any errors.

If the above setup didn't work well, you will typically get the error

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

Hope that helps someone save a 6 hours that I wasted !


Well the solution is too easy to see. Simply delete the @Type annotation, so the declaration looks like this:

@Column(columnDefinition = "geometry(Point,4326)")
private Point position;

Source:

Note the @Type annotation. This informs Hibernate that the location attribute is of type Geometry. The @Type annotation is Hibernate specific, and the only non-JPA annotation that is required. (In future versions of Hibernate (version 5 and later) it will no longer be necessary to explicitly declare the Type of Geometry-valued attributes.)


The solution suggested by Denis above did not work for me on Hiberate 5 spatial and Mysql. However the following annotations worked for me

  @Column(name = "location",columnDefinition="Geometry")
  private Geometry location;


  @Column(name = "pointlocation",columnDefinition="Point")
  private Point pointlocation;

If you are running Hibernate 5.3+ you can skip out on column definitions

  @Column(name = "location")
  private Geometry location;


  @Column(name = "pointlocation")
  private Point pointlocation;