java Hibernate Spatial 5 - GeometryType
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31440496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate Spatial 5 - GeometryType
提问by Denis Lukenich
After upgrading Hibernate-spatial to Version 5.0.0.CR2 the following declaration doesn't work anymore:
将 Hibernate-spatial 升级到版本 5.0.0.CR2 后,以下声明不再起作用:
@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?
正如我所看到的,Jar 文件中不再存在该类。GeometryType 发生了什么变化以及如何替换它?或者是否有另一个 jar 文件要包含?
Edit: For clarification. I am using Hibernate-Spatial in Combination with a PostgreSQL-Postgis database.
编辑:为了澄清。我将 Hibernate-Spatial 与 PostgreSQL-Postgis 数据库结合使用。
回答by Denis Lukenich
Well the solution is too easy to see. Simply delete the @Type annotation, so the declaration looks like this:
那么解决方案太容易看到了。只需删除@Type 注释,声明如下所示:
@Column(columnDefinition = "geometry(Point,4326)")
private Point position;
来源:
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.)
注意@Type 注释。这会通知 Hibernate location 属性的类型为 Geometry。 @Type 注释是特定于 Hibernate 的,并且是唯一需要的非 JPA 注释。 (在 Hibernate 的未来版本(版本 5 及更高版本)中,不再需要显式声明几何值属性的类型。)
回答by Ganesh Krishnan
The solution suggested by Denis above did not work for me on Hiberate 5 spatial and Mysql. However the following annotations worked for me
上面 Denis 建议的解决方案在 Hiberate 5 spatial 和 Mysql 上对我不起作用。但是以下注释对我有用
@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
如果您运行的是 Hibernate 5.3+,则可以跳过列定义
@Column(name = "location")
private Geometry location;
@Column(name = "pointlocation")
private Point pointlocation;
回答by Wisienkas
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.
所以我一直在与这个问题作斗争几天,问题是我希望数据库类型来自 postgis 类型,以便我为最近的邻居等运行有效的距离查询。我终于想出了如何去做并想与社区分享,所以我创建了一个包含问题解决方案的演示项目。
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 Hymanson.
我的设置是带有 Postgis、Spring boot jpa、Hibernate 5+、Hibernate-spatial 5+ 的 PostgreSQL 数据库,并且还添加了转换为 rest 输出的功能,这再次需要 Hymanson 的特殊模块。
the project is found at https://github.com/Wisienkas/springJpaGeo
该项目位于https://github.com/Wisienkas/springJpaGeo
the important code you was asking for was this:
您要求的重要代码是:
@type(type = "jts_geometry")
private Point point;
回答by HopeKing
For Hibernate Spatial 5.2.x, all you need is the below in your entity.
对于 Hibernate Spatial 5.2.x,您所需要的只是实体中的以下内容。
private Point location;
private Point location;
You don't need columnDefinition or Type like the above solutions mentioned.
您不需要像上面提到的解决方案那样的 columnDefinition 或 Type 。
Some additional details and checks to see if the above works
一些额外的细节并检查以上是否有效
- As soon as you run your setup and use
desc table_name
in 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.
- 一旦您
desc table_name
在 mysql 中运行您的设置并使用 ,您应该会看到geometry的字段类型。这表明您的休眠到数据库映射工作正常。 - 现在尝试从 java 创建一个实体和 repo.save(entity) 并且您的实体应该可以正常保存而没有任何错误。
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
数据截断:无法从发送到 GEOMETRY 字段的数据中获取几何对象 Blockquote
Hope that helps someone save a 6 hours that I wasted !
希望能帮助别人节省我浪费的 6 个小时!
回答by Paulito.Bandito
Apparently your Hibernate libraries need to be at the same version too.
显然,您的 Hibernate 库也需要处于同一版本。
I was using postgis / springboot / hibernate-spatial.
我正在使用 postgis/springboot/hibernate-spatial。
Initially Hibernate-Spatial was @ 5.4.10.Final, and didn't work even with the solutions here.
最初 Hibernate-Spatial 是@ 5.4.10.Final,即使使用这里的解决方案也不起作用。
I then looked at what version of hibernate was in my maven dependencies ( hibernate-commons-annotations-5.1.0.Final.jar) and remembered seeing somewhere that the versions of hibernate need to match.
然后我查看了我的 maven 依赖项 (hibernate-commons-annotations-5.1.0.Final.jar) 中的 hibernate 版本,并记得看到某个地方需要匹配 hibernate 的版本。
So I downgraded my Hibernate-Spatial, to 5.1 and the following mapping statement works without any more info:
所以我将我的 Hibernate-Spatial 降级到 5.1 并且以下映射语句在没有任何更多信息的情况下工作:
// Geometry Object is from the following import
import com.vividsolutions.jts.geom.*;
@Column(name="domain_loc")
private Geometry location;
回答by Hasnu zama
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
我正在使用休眠 4.3.6 和休眠空间 5.2.2。上面建议的解决方案对我不起作用。但是下面的代码有效如果我添加休眠空间 4.3
@Type(type="org.hibernate.spatial.GeometryType")
private Point location;