-->
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.  [ 3 posts ] 
Author Message
 Post subject: Limit child collection when setMaxResult is used on parent
PostPosted: Thu Jan 07, 2010 10:05 am 
Newbie

Joined: Thu Jan 07, 2010 9:58 am
Posts: 4
I have a parent entity with a @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER) child collection.
When querying the parent using setMaxResults I get sql with the "limit" sql keyword.
After that the sql generated by hibernate to fetch the children doesn't have the limit keyword.
So I will get all the children of all the parents (without the limit) out of the database.

That is not good.

What can I do to limit the fetching of the children?
Any help/comments are appreciated.
Thanks in advance.

Best regards Trym


Top
 Profile  
 
 Post subject: Re: Limit child collection when setMaxResult is used on parent
PostPosted: Thu Jan 07, 2010 12:50 pm 
Newbie

Joined: Thu Nov 12, 2009 4:32 pm
Posts: 16
can you please post the code you have


Top
 Profile  
 
 Post subject: Re: Limit child collection when setMaxResult is used on parent
PostPosted: Fri Jan 08, 2010 5:14 am 
Newbie

Joined: Thu Jan 07, 2010 9:58 am
Posts: 4
E.g.
Generated sql:
Code:
1.    select
        parent.pid as pid2_,
        ...
    from
        Parent parent
    where
        (
            1=1
        )
        and (
            parent.attribute1 is null
        )
        and (
            parent.attribute2 is null
            or parent.attribute2<?
        )
    order by
        parent.pid limit ?|#]
2.     select
        child.parent_pid as parentpid_1_,
        child.pid as pid1_,
        ...
    from
        Child child
    where
        (
            1=1
        )
        and child.parent_pid in (
            select
                parent.pid
            from
                Parent parent
            where
                (
                    1=1
                )
                and (
                  parent.attribute1 is null
                )
                and (
                  parent.attribute2 is null
                  or parent.attribute2<?
                )
        )|#]

Note the missing limit in the last where clause.
The problem arises because of the fetchtype=eager on the child relation.

Is there any way to disable this fetchtype on a single query (the one with setMaxResults)?

Best regards Trym


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