Limiting the numbers of results returned by a Hibernate query when using direct set association

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Limiting the numbers of results returned by a Hibernate query when using direct set association

elvanor
Hello,

I have a class Category and a class Item that has a many to one
relationship to Category, such as the field myCategory.items
represents the list of items that a category has.

In a controller query I need to find a category, then return some of
the items (let's say the first 10) that the category has. Once I have
the category object I could just use

 myCategory.items[0..10]

But I am worried that Grails will not be smart enough to tell
Hibernate to only fetch the 10 first elements. I believe Hibernate
will fetch the whole collection in memory and then Groovy will just
truncate it, which can pose performance problems.

Is there any way I can be sure such a situation does not happen, while
still using the class association that Category has to items?

Jean-Noel

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Limiting the numbers of results returned by a Hibernate query when using direct set association

Marc Palmer Local

On 4 Aug 2007, at 15:59, Jean-Noël Rivasseau wrote:

> Hello,
>
> I have a class Category and a class Item that has a many to one
> relationship to Category, such as the field myCategory.items
> represents the list of items that a category has.
>
> In a controller query I need to find a category, then return some of
> the items (let's say the first 10) that the category has. Once I have
> the category object I could just use
>
>  myCategory.items[0..10]
>
> But I am worried that Grails will not be smart enough to tell
> Hibernate to only fetch the 10 first elements. I believe Hibernate
> will fetch the whole collection in memory and then Groovy will just
> truncate it, which can pose performance problems.
>
I think the solution is to use a criteria to perform the query.  
Criteria are great, especiall projections! You even query just the  
properties you need instead of getting all the properties of all the  
associated objects.

Marc


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: Limiting the numbers of results returned by a Hibernate query when using direct set association

Marcos Silva Pereira
Anyway, the idiom used in "myCategory.items[0..10]" is nice. What about support it?

Kind Regards

On 8/4/07, Marc Palmer <[hidden email]> wrote:

On 4 Aug 2007, at 15:59, Jean-Noël Rivasseau wrote:

> Hello,
>
> I have a class Category and a class Item that has a many to one
> relationship to Category, such as the field myCategory.items
> represents the list of items that a category has.
>
> In a controller query I need to find a category, then return some of
> the items (let's say the first 10) that the category has. Once I have
> the category object I could just use
>
>  myCategory.items[0..10]
>
> But I am worried that Grails will not be smart enough to tell
> Hibernate to only fetch the 10 first elements. I believe Hibernate
> will fetch the whole collection in memory and then Groovy will just
> truncate it, which can pose performance problems.
>
I think the solution is to use a criteria to perform the query.
Criteria are great, especiall projections! You even query just the
properties you need instead of getting all the properties of all the
associated objects.

Marc


---------------------------------------------------------------------
To unsubscribe from this list please visit:

     http://xircles.codehaus.org/manage_email




--
Marcos Silva Pereira
http://marcospereira.wordpress.com
"You only live once.", James Brown
Reply | Threaded
Open this post in threaded view
|

Re: Limiting the numbers of results returned by a Hibernate query when using direct set association

elvanor
Hi,

I actually realised this idiom would not work as it is. This is
because myCategory.items is an Hibernate implementation of a set
(PersistentHibernateSet is the class name or something like that).
When I used the [0..10] slicing operator I got a Groovy error.

So what I did was convert first myCategory.items to a Groovy Set and
then use the operator. But I fear, like I said, that Hibernate is
actually fetching all the items even if they are thousands of them. I
am not sure though - what Hibernate is actually doing is not evident.
It could fill the collection with proxies. But then it would fetch
from the DB when converting to a set I believe.

Jean-Noel

On 8/6/07, Marcos Silva Pereira <[hidden email]> wrote:

> Anyway, the idiom used in "myCategory.items[0..10]" is nice. What about
> support it?
>
> Kind Regards
>
>
> On 8/4/07, Marc Palmer < [hidden email]> wrote:
> >
> > On 4 Aug 2007, at 15:59, Jean-Noël Rivasseau wrote:
> >
> > > Hello,
> > >
> > > I have a class Category and a class Item that has a many to one
> > > relationship to Category, such as the field myCategory.items
> > > represents the list of items that a category has.
> > >
> > > In a controller query I need to find a category, then return some of
> > > the items (let's say the first 10) that the category has. Once I have
> > > the category object I could just use
> > >
> > >  myCategory.items[0..10]
> > >
> > > But I am worried that Grails will not be smart enough to tell
> > > Hibernate to only fetch the 10 first elements. I believe Hibernate
> > > will fetch the whole collection in memory and then Groovy will just
> > > truncate it, which can pose performance problems.
> > >
> > I think the solution is to use a criteria to perform the query.
> > Criteria are great, especiall projections! You even query just the
> > properties you need instead of getting all the properties of all the
> > associated objects.
> >
> > Marc
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >      http://xircles.codehaus.org/manage_email
> >
> >
>
>
>
> --
> Marcos Silva Pereira
> http://marcospereira.wordpress.com
> "You only live once.", James Brown
Reply | Threaded
Open this post in threaded view
|

Re: Limiting the numbers of results returned by a Hibernate query when using direct set association

Marcos Silva Pereira
You could set the fetch size via hibernate mappings:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="org.foo.bar.Category">
        ...
        <set name="items" batch-size="10">
            <key column="category"/>
            <one-to-many class="org.foo.bar.Item" />
        </set>
    </class>
</hibernate-mapping>

So, hibernate will retrieve items 10 by 10 (using a in clause). AFAIK, GORM is not ready to do this kind of mapping.

Kind Regards,

On 8/7/07, Jean-Noël Rivasseau <[hidden email]> wrote:
Hi,

I actually realised this idiom would not work as it is. This is
because myCategory.items is an Hibernate implementation of a set
(PersistentHibernateSet is the class name or something like that).
When I used the [0..10] slicing operator I got a Groovy error.

So what I did was convert first myCategory.items to a Groovy Set and
then use the operator. But I fear, like I said, that Hibernate is
actually fetching all the items even if they are thousands of them. I
am not sure though - what Hibernate is actually doing is not evident.
It could fill the collection with proxies. But then it would fetch
from the DB when converting to a set I believe.

Jean-Noel

On 8/6/07, Marcos Silva Pereira < [hidden email]> wrote:

> Anyway, the idiom used in "myCategory.items[0..10]" is nice. What about
> support it?
>
> Kind Regards
>
>
> On 8/4/07, Marc Palmer < [hidden email]> wrote:
> >
> > On 4 Aug 2007, at 15:59, Jean-Noël Rivasseau wrote:
> >
> > > Hello,
> > >
> > > I have a class Category and a class Item that has a many to one
> > > relationship to Category, such as the field myCategory.items
> > > represents the list of items that a category has.
> > >
> > > In a controller query I need to find a category, then return some of
> > > the items (let's say the first 10) that the category has. Once I have
> > > the category object I could just use
> > >
> > >  myCategory.items[0..10]
> > >
> > > But I am worried that Grails will not be smart enough to tell
> > > Hibernate to only fetch the 10 first elements. I believe Hibernate
> > > will fetch the whole collection in memory and then Groovy will just
> > > truncate it, which can pose performance problems.
> > >
> > I think the solution is to use a criteria to perform the query.
> > Criteria are great, especiall projections! You even query just the
> > properties you need instead of getting all the properties of all the
> > associated objects.
> >
> > Marc
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >       http://xircles.codehaus.org/manage_email
> >
> >
>
>
>
> --
> Marcos Silva Pereira
> http://marcospereira.wordpress.com
> "You only live once.", James Brown



--
Marcos Silva Pereira
http://marcospereira.wordpress.com
"You only live once.", James Brown
Reply | Threaded
Open this post in threaded view
|

Re: Limiting the numbers of results returned by a Hibernate query when using direct set association

Ricardo J. Méndez
In reply to this post by elvanor
Check out the relationship paging pattern that Clay Luther proposed:

http://www.nabble.com/A-Relationship-Paging-Pattern-tf3873668.html#a10975698



On 8/7/07, Jean-Noël Rivasseau <[hidden email]> wrote:
Hi,

I actually realised this idiom would not work as it is. This is
because myCategory.items is an Hibernate implementation of a set
(PersistentHibernateSet is the class name or something like that).
When I used the [0..10] slicing operator I got a Groovy error.

So what I did was convert first myCategory.items to a Groovy Set and
then use the operator. But I fear, like I said, that Hibernate is
actually fetching all the items even if they are thousands of them. I
am not sure though - what Hibernate is actually doing is not evident.
It could fill the collection with proxies. But then it would fetch
from the DB when converting to a set I believe.

Jean-Noel

On 8/6/07, Marcos Silva Pereira < [hidden email]> wrote:

> Anyway, the idiom used in "myCategory.items[0..10]" is nice. What about
> support it?
>
> Kind Regards
>
>
> On 8/4/07, Marc Palmer < [hidden email]> wrote:
> >
> > On 4 Aug 2007, at 15:59, Jean-Noël Rivasseau wrote:
> >
> > > Hello,
> > >
> > > I have a class Category and a class Item that has a many to one
> > > relationship to Category, such as the field myCategory.items
> > > represents the list of items that a category has.
> > >
> > > In a controller query I need to find a category, then return some of
> > > the items (let's say the first 10) that the category has. Once I have
> > > the category object I could just use
> > >
> > >  myCategory.items[0..10]
> > >
> > > But I am worried that Grails will not be smart enough to tell
> > > Hibernate to only fetch the 10 first elements. I believe Hibernate
> > > will fetch the whole collection in memory and then Groovy will just
> > > truncate it, which can pose performance problems.
> > >
> > I think the solution is to use a criteria to perform the query.
> > Criteria are great, especiall projections! You even query just the
> > properties you need instead of getting all the properties of all the
> > associated objects.
> >
> > Marc
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe from this list please visit:
> >
> >       http://xircles.codehaus.org/manage_email
> >
> >
>
>
>
> --
> Marcos Silva Pereira
> http://marcospereira.wordpress.com
> "You only live once.", James Brown



--


Ricardo J. Méndez
http://ricardo.strangevistas.net/