-->
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.  [ 6 posts ] 
Author Message
 Post subject: eager fetching data in hql
PostPosted: Tue Jan 15, 2008 1:44 pm 
Newbie

Joined: Mon Jan 14, 2008 9:57 am
Posts: 5
does anybody know how i can fetch only part of the information in hql
in the following scenario:

class A has a collection of class B.

class A
{
Collection<B> bees;
}

and i want to check an equality on the "bees" part.

something like the following:

================

function: isEqual
-------------------
A a1;
A a2;

if(a1.bees.size() != a2.bees.size())
return false;

Iterator<B> b1 = a1.getBees();
Iterator<B> b2 = a2.getBees();
while(b1.hasNext())
{
if(b1.next().getId() != b2.next().getId())
return false;
}

return true;

==========================


and the HQL is something like the followings:

select a from A a where a.id=8

- i dont want to fetch all the "bees" part in the HQL.
B is very big one (even when i get only the primitive part)
- i just want to compare the ids.
- the "size()" part also causes for lazy loading.
- comment: the id will always exist in the equal function.


thanks.


Top
 Profile  
 
 Post subject: Re: eager fetching data in hql
PostPosted: Tue Jan 15, 2008 2:36 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I am a little lost here. what do you want the HQL do for you? get two As and tell you whether they have the same B collections?


Farzad-


Top
 Profile  
 
 Post subject: Re: eager fetching data in hql
PostPosted: Tue Jan 15, 2008 2:43 pm 
Newbie

Joined: Mon Jan 14, 2008 9:57 am
Posts: 5
i want the HQL return all the ids of the "bees",
so no additional query will be when i perform the following:
b1.next().getId() != b2.next().getId()

The bees part is "lazy".

i also dont want all the bees data to return in the query.


and of course the "size()" part will not fetch also the "bees" data.
i dont need it.


Top
 Profile  
 
 Post subject: Re: eager fetching data in hql
PostPosted: Tue Jan 15, 2008 4:46 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
The only thing that comes across my mind is that you force fetching the collection by doing a fetch join as shown below:

Code:
select a from A a inner join fetch a.bees where a.id=8


and for B you set all the attributes except the identifier to be lazy loaded. Therefore, when that query runs it loads the collection but only the ids for bees and it all executes in one query. Note you will have to instrument your compiled code so that property lazy loading works. Refer to "19.1.7. Using lazy property fetching" of hibernate document if you need more information on property lazy loading.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 6:11 pm 
Newbie

Joined: Tue Jan 15, 2008 10:07 am
Posts: 14
Wouldn't the "extra-lazy" fetching strategy be helpfull in this instance?
I'm not sure, but it might be usefull to check out the Primer On Fetching Strategies for details on the subject.


Top
 Profile  
 
 Post subject: the "extra" seems to does it.
PostPosted: Thu Jan 17, 2008 8:08 am 
Newbie

Joined: Mon Jan 14, 2008 9:57 am
Posts: 5
thanks !

is there a way to fetch something in HQL with "extra data" on some field,
like i can do with "eager" (and the field is mapped as "lazy") ?


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