Hibernate Spatial 5 - GeometryType

匆匆过客 提交于 2019-12-03 08:24:33

问题


After upgrading Hibernate-spatial to Version 5.0.0.CR2 the following declaration doesn't work anymore:

@Column(columnDefinition = "geometry(Point,4326)")
@Type(type = "org.hibernate.spatial.GeometryType")
private Point position;

with an:

org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.spatial.GeometryType]

As I can see the class doesn't exist in the Jar-File anymore. What happend to the GeometryType and how is it replaced? Or is there another jar-file to include?

Edit: For clarification. I am using Hibernate-Spatial in Combination with a PostgreSQL-Postgis database.


回答1:


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.)




回答2:


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;



回答3:


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 !




回答4:


So I've been battling this problem for a few days, the thing is that I wanted the database type to be from the postgis type in order for me to run efficient distance queries for nearest neighbour etc. I finally figured out how to do it and wanted to share it with the community so I created a demo project containing the solution to the problem.

My setup is a PostgreSQL database with Postgis, Spring boot jpa, Hibernate 5+, Hibernate-spatial 5+ and also added converting to rest output, which again required a special module from jackson.

the project is found at https://github.com/Wisienkas/springJpaGeo

the important code you was asking for was this:

@type(type = "jts_geometry")
private Point point;



回答5:


I am using hibernate 4.3.6 and hibernate spatial 5.2.2. The solution suggested above didn't work for me. However the below code worked If I add hibernate spatial 4.3

@Type(type="org.hibernate.spatial.GeometryType")
private Point location;


来源:https://stackoverflow.com/questions/31440496/hibernate-spatial-5-geometrytype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!