-->
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: Criteria, Projections, and lazy-fetch collections.
PostPosted: Thu Sep 15, 2005 9:56 am 
Newbie

Joined: Thu Jun 02, 2005 11:20 am
Posts: 9
All,

Hibernate 3.0.5.... trying to project a lazy-fetched collection via a Criteria query:

Code:
List authors =
   session.createCriteria(Content.class)
     .setProjection(Projections.property("authors"))
     .list();

System.out.println("authors: " + authors);


Authors is a lazy-fetched List... and when I execute this, I get a list of nulls:

authors: [null, null, null, null, null, null, null, null]

Here's the SQL that's executed, it looks like it's just querying for the available Content, then calling getAuthors() on it, but the CGLIB proxy isn't initialized, so it's just getting null:

Code:
select this_.content_id as y0_ from iwsc_content this_ left outer join iwsc_news this_1_ on this_.content_id=this_1_.content_id left outer join iwsc_event this_2_ on this_.content_id=this_2_.content_id left outer join iwsc_document this_3_ on this_.content_id=this_3_.content_id


Even after I disabled lazy fetching, the SAME sql is executed.

However, if I remove the projection:

Code:
List authors =
   session.createCriteria(Content.class)
//     .setProjection(Projections.property("authors"))
     .list();

System.out.println("authors: " + authors);


I get the correct SQL and things work properly: ( this is w/ lazy=false, however it also works w/ lazy authors ).

Code:
Hibernate: select this_.content_id as content1_2_0_, this_.content_type_id as content2_2_0_, this_.title as title2_0_, this_.summary as summary2_0_, this_.branch as branch2_0_, this_.relative_path as relative6_2_0_, this_.image_path as image7_2_0_, this_.creation_date as creation8_2_0_, this_.expiration_date as expiration9_2_0_, this_.last_update_date as last10_2_0_, this_.last_update_user as last11_2_0_, this_1_.location as location4_0_, this_1_.date as date4_0_, this_2_.location as location5_0_, this_2_.start_date as start3_5_0_, this_2_.end_date as end4_5_0_, this_3_.file_size as file2_6_0_, case when this_1_.content_id is not null then 1 when this_2_.content_id is not null then 2 when this_3_.content_id is not null then 3 when this_.content_id is not null then 0 end as clazz_0_ from iwsc_content this_ left outer join iwsc_news this_1_ on this_.content_id=this_1_.content_id left outer join iwsc_event this_2_ on this_.content_id=this_2_.content_id left outer join iwsc_document this_3_ on this_.content_id=this_3_.content_id
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?
Hibernate: select authors0_.content_id as content1_1_, authors0_.priority as priority1_, authors0_.content_id as content1_8_0_, authors0_.priority as priority8_0_, authors0_.email as email8_0_, authors0_.first_name as first4_8_0_, authors0_.last_name as last5_8_0_ from iwsc_author authors0_ where authors0_.content_id=?

authors: [Content{100000000}, Content{100000005}, Content{100000010}, News{100000015}, News{100000020}, Event{100000025}, Event{100000030}, Document{100000035}]


I honestly think this is a bug, unless HB isn't supposed to support Projected collections...

I _really_ love the Criteria API and have to build dynamic queries at runtime, so I'd hate to have to use Query here... please help!

Thanks in advance,

Shanon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 2:56 pm 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
I have same problem! :(

_________________
Please don't forget to rateME!


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.