oracle 在 Hibernate 中映射计算字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1594691/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:15:24  来源:igfitidea点击:

Mapping Calculated Fields in Hibernate

javaoraclehibernateormspatial

提问by DrewEaster

I'm not sure if this is possible in Hibernate but it would be really great if it was :) I've not actually got past the conceptual stage with this but I'll explain as best as I can.

我不确定这在 Hibernate 中是否可行,但如果是这样就太好了:) 我实际上还没有通过这个概念阶段,但我会尽我所能解释。

I want to make use of Oracle Spatial features to do proximity based searching. Imagine I've got a Location entity which stores a latitude/longitude value. Then imagine I want to query for all locations within 5km of a user specified latitude/longitude location. In the results, I want to see all matching Locations but, in addition to the standard mapped fields on the Location entity, I also want to present the distance of each Location relative to the user specified location.

我想利用 Oracle Spatial 功能进行基于邻近度的搜索。想象一下,我有一个 Location 实体,用于存储纬度/经度值。然后想象我想查询用户指定的纬度/经度位置 5 公里内的所有位置。在结果中,我想查看所有匹配的位置,但是,除了 Location 实体上的标准映射字段之外,我还想显示每个位置相对于用户指定位置的距离。

Oracle Spatial would let me do this as a calculated field in SQL but I don't understand how Hibernate can support calculated fields that get returned from the database. As the calculated field is not a column in the table, I can't do a standard mapping.

Oracle Spatial 允许我将其作为 SQL 中的计算字段执行此操作,但我不明白 Hibernate 如何支持从数据库返回的计算字段。由于计算字段不是表中的列,我无法进行标准映射。

Is there some special features that allow me to create wrappers for POJOs and have Hibernate map to them such that additional calculated properties can be returned?

是否有一些特殊功能允许我为 POJO 创建包装器并将 Hibernate 映射到它们以便可以返回额外的计算属性?

回答by Chris M.

I've found, using JPA annotations, that I can simply create a property on the mapped object with the same name as your calculated sql field, (e.g. "distance") and annotate it as:

我发现,使用 JPA 注释,我可以简单地在映射对象上创建一个与计算的 sql 字段同名的属性(例如“距离”)并将其注释为:

@Column(insertable=false, updatable=false)
private Double distance;

And this should map property and not throw an error when you're trying to save back from the DB.

这应该映射属性,并且在您尝试从数据库中保存时不会抛出错误。

please note that the correct annotation is updatable (not updateable as it was posted originally)

请注意,正确的注释是可更新的(不可更新,因为它最初发布)

回答by Pascal Thivent

Did you look at Hibernate Spatialand its Oracle10g/11g Provider?

你看过Hibernate Spatial和它的Oracle10g/11g Provider吗?

Hibernate Spatial is a generic extension to Hibernate for handling geographic data. Hibernate Spatial is open source and licensed, like Hibernate, under the LGPL license.

Hibernate Spatial allows you to deal with geographic data in a standardized way. It abstracts away from the specific way your database supports geographic data, and provides a standardized, cross-database interface to geographic data storage and query functions.

Hibernate Spatial supports most of the functions of the OGC Simple Feature Specification. Supported databases are: Oracle 10g/11g, Postgresql/Postgis, and MySQL.

Hibernate Spatial 是 Hibernate 的通用扩展,用于处理地理数据。Hibernate Spatial 是开源的,和 Hibernate 一样,是在 LGPL 许可下获得许可的。

Hibernate Spatial 允许您以标准化的方式处理地理数据。它从您的数据库支持地理数据的特定方式中抽象出来,并为地理数据存储和查询功能提供了标准化的跨数据库接口。

Hibernate Spatial 支持 OGC Simple Feature Specification 的大部分功能。支持的数据库有:Oracle 10g/11g、Postgresql/Postgis 和 MySQL。