-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Possible to make criteria return field values?
PostPosted: Fri Aug 29, 2008 10:30 am 
Newbie

Joined: Thu Nov 16, 2006 11:52 am
Posts: 8
Location: Frankfurt a.M., Germany
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


Top
 Profile  
 
 Post subject: Re: Possible to make criteria return field values?
PostPosted: Fri Aug 29, 2008 11:42 am 
Newbie

Joined: Thu Aug 28, 2008 3:31 am
Posts: 5
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.