OlliP wrote:
Hello,
I like criteria queries in hibernate quite a bit and would like to use them for a specific query problem. I need the query to return only the values of individual fields in a table (e.g. the primary keys) and not the entire row mapped to some Java object. Reading various books and docs I found no way to do this with criteria queries. All sample code returns lists of whole objects rather than the values of individual fields.
Problem is that my query needs to return matching entries from several tables. So I need to have one criteria query for every table. In a second step these matching entries have to be filtered by various AND and OR conditions. Only the survivors need to be really fetched from the database as whole objects to minimize memory consumption. That is why I only want the 1st level queries to return the primary keys, with which I can lateron retrieve the survivors as whole objects.
Thanks for any hints.
Regards, Oliver
I think Criteria can meet your requirements.
For instance:
There is an entity class definition, A.class, with primary key attributes key1,key2 and ordinary property p0 and B.class with keyB0 and property pB0.
If you want to query key1 and key2 like this sql,
Code:
select key1,key2 from table_a,table_b where p0=pB0.
(Just a sample), the corresponding criteria query should be,
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(A.class);
criteria.setProjection(Projections.projectionList().add(Projections.property("key1").add(Projections.property("key2")));
DetachedCriteria subQueryCriteria = DetachedCriteria.forClass(B.class)
subQueryCriteria.setProjection(Projections.property("pB0"));
criteria.add(SubQueries.propertyIn("p0",subQueryCriteria));
Hope my reply is helpful to you.