开发者

Mapping Calculated Fields in Hibernate

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.

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

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?


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:

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


Did you look at Hibernate Spatial and its 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜