views:

956

answers:

2

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?

+2  A: 

If you're using Hibernate XML mappings, you can use <formula> as a replacement for <column>

http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html#mapping-column

If using JPA annotations, there's a Hibernate extension annotation called @Formula:

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e2273

skaffman
I'm not sure this is quite what I need. The calculated field, in this case, is not just a simple calculation based on the values of the available fields. It is based on a function that determines proximity given a specified user location. The @formula annotation only seems to allow me to work with data that already exists in the table, not data that is calculated via a special function.
drewzilla
+1  A: 

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.

Pascal Thivent
This seems very interesting and thank you for pointing me in this direction. It is still not clear, however, how I'd actually map calculated fields. Imagine my Location class has a "Point" defined. I want to return a list of Locations that are within 5km of a user specified Point. I want each matching Location to include the exact proximity to the user specified point (this must have been calculated in the database as part of the geometric processing). My basic Location POJO obviously doesn't have this calculated column. If it is returned in the query, how do I map it?
drewzilla