-->
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.  [ 4 posts ] 
Author Message
 Post subject: fetch one-to-many association using Criteria
PostPosted: Wed Apr 15, 2009 4:23 pm 
Newbie

Joined: Wed Apr 15, 2009 3:45 pm
Posts: 2
I am a Hibernate newbie, and trying to see whether it is possible to use Criteria projections to return a collection of one-to-many associated objects, and cannot get the data. My entity mapping is like below:

Class A <n----1> Class B <1----n> Class C

I am using

Criteria criteria = createCriteria(A.class).
.createAlias("B", "B")
.createAlias("B.C", "C")
.setProjection(Projections.projectionList().
.add(Projections.property("B.f1")
.add(Projections.Property("B.C");
List<Object[]>rows = criteria.list();

I am expecitng the rows[*].[0] is f1 value, and rows[*].[1] is List<C>, a collection of C objects. But I cannot get back any rows[*].[1].

Any suggestions?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 5:56 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
If ClassB contains ClassC, and the mapping is defined, you only need to select ClassB. Instances of ClassC will be populated based on the mapping. Is that what you are trying to do?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 8:53 pm 
Newbie

Joined: Wed Apr 15, 2009 3:45 pm
Posts: 2
LinHib,

Thanks for the reply. B defines a one-to-many mapping to C (which is another entity). The reason I do not want to pull up B fully is because B has many many other fields/collections which we do not need, and it takes a long time to fill these values if we put B in the result projection (it seems to try to compute all B properties/collections then).

In our situation, we want to extract all (a large number of) the As in database. Since we only need A.B.f1 (a string value) and A.B.C (List of C objects), we want to know how we can retrieve and fill just these two properties to improve performance.

Is there anyway we can achieve this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 6:52 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
If you use lazy loading for B (by setting default-lazy="true" in the hibernate-mapping), and do not use lazy (by setting lazy="false") for the association to C, you can build B with only C populated (without loading any other associated entities).

---
please rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.