views:

1090

answers:

2

In the hibernate mapping file I use property formula to get any value.

<property name="price" type="double" formula="(select SUM(amount) ...) />

But it allows to get only 1 row. How can I get lisf of rows? I understand that "property" is not suitable, but it's impossible to add formula to set or list.

Of course I just want to get values without adding new classes. I just have the sql request and expect to get list or set for existed class.

+1  A: 

Formula properties only operate on the current row. If you want to perform aggregate functions, you need to write a specific query to fetch them. See the section of the hibernate docs that handle aggregate functions.

skaffman
Yeah, I can write a query which returns more tnen 1 row, but how can I store this result in my class? And I want to set this query in my mapping xml file related with my class.
Mikhail.Mamaev
You can't, that's what I'm saying. You can't have a property of a row which contains data from multiple rows, that doesn't make sense.
skaffman
I'm not saying that I want exactly property. Let it be list or set or something else, doesn't matter, but how?
Mikhail.Mamaev
+2  A: 

Formulas apply to single-valued properties only; not to collections. You can, however, specify a custom SQL statement to be used for loading a collection.

The tricky part here is that from your question it sounds like what you want is a collection of elements (double price) rather than entities; I've never tried to specify custom loader for something like that and so am not sure whether it will work. Documentation is rather silent on that issue.

If the above does not work, another option is to define your SELECT as a view and map collection that way (again, as collection of elements).

Finally, you can also run a query like skaffman suggested. You'll need to invoke that query and populate a collection on your entity manually when appropriate (e.g. within your DAO after entity is loaded) ro you can define an appropriate listener and do it there.

ChssPly76