I came across this while trying out some optimization strategies for Hibernate, version used is 3.2.6GA
Code:
Query query = session.createQuery("from User u");
query.setMaxResults(1000);
ArrayList<User> users = (ArrayList<User>) query.list();
for (User user : users) {
String s = user.getProducts().iterator().next().getName();
}
user->products is mapped with fetch="subselect":
Code:
<set name="products" table="USER_PRODUCTS" cascade="all" fetch="subselect">
<key column="user_id" />
<many-to-many column="product_Id" class="data.Product"/>
</set>
The resulting queries are:
Quote:
Hibernate:
select
user0_.ID as ID0_,
user0_.username as username0_,
user0_.password as password0_,
user0_.title as title0_,
user0_.firstname as firstname0_,
user0_.lastname as lastname0_,
user0_.email as email0_,
user0_.account_id as account8_0_
from
USERS user0_ limit ?
Hibernate:
select
products0_.user_id as user1_1_,
products0_.product_Id as product2_1_,
product1_.ID as ID3_0_,
product1_.name as name3_0_,
product1_.price as price3_0_
from
USER_PRODUCTS products0_
left outer join
PRODUCTS product1_
on products0_.product_Id=product1_.ID
where
products0_.user_id in (
select
user0_.ID
from
USERS user0_
)
So the setMaxResults affected the query on users, but not the subselect to get the products.
FAQ, documentation and forum search turned up nothing that helped.
The only reference i found of this is
http://opensource.atlassian.com/projects/hibernate/browse/HHH-304
Is this behaviour known and accepted or did i miss something?
any feedback appreciated
Patrik