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