HQL on collections

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

HQL on collections

pronto_mike
I have the following Domain classes:

Instructor
Class
School

Instructors have a collection of Classes and a class is associated to a school.

I need to be able to query for all instructors in a school. I've tried
the following and it's not working:

'Select i from Instructor i, Course c where c.school  = :school and c
in elements(i.courses)'

I've tried other variations without any luck. Any suggestions?

-Mike

BTW, I'm doing this in grails 1.1.1. I assume it shouldn't matter though.

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: HQL on collections

Nicolás Dijkstra
have you tried with

Instructor.withCriteria {
    classes {
       school {
           idEq(schoolId)
       }
    }
}

Hope it helps!


On Sat, Nov 14, 2009 at 5:14 AM, Michael Masters <[hidden email]> wrote:
I have the following Domain classes:

Instructor
Class
School

Instructors have a collection of Classes and a class is associated to a school.

I need to be able to query for all instructors in a school. I've tried
the following and it's not working:

'Select i from Instructor i, Course c where c.school  = :school and c
in elements(i.courses)'

I've tried other variations without any luck. Any suggestions?

-Mike

BTW, I'm doing this in grails 1.1.1. I assume it shouldn't matter though.

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

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: HQL on collections

pronto_mike
No luck :(

I just realized my description was a little confusing. I was using
Course and Class interchangeably. Let me be a little more explicit
about the domain objects.

class Instructor {
    String firstName
    String lastName
    String middleInitial

    static hasMany = [ courses:Course ]
}

class Course {
    School school
    static belongsTo = [ instructor:Instructor ]
}

class School {
    String name
    State state
    String aliases
}

Thanks,
Mike






2009/11/14 Nicolás Dijkstra <[hidden email]>:

> have you tried with
> Instructor.withCriteria {
>     classes {
>        school {
>            idEq(schoolId)
>        }
>     }
> }
>
> Hope it helps!
>
> On Sat, Nov 14, 2009 at 5:14 AM, Michael Masters <[hidden email]> wrote:
>>
>> I have the following Domain classes:
>>
>> Instructor
>> Class
>> School
>>
>> Instructors have a collection of Classes and a class is associated to a
>> school.
>>
>> I need to be able to query for all instructors in a school. I've tried
>> the following and it's not working:
>>
>> 'Select i from Instructor i, Course c where c.school  = :school and c
>> in elements(i.courses)'
>>
>> I've tried other variations without any luck. Any suggestions?
>>
>> -Mike
>>
>> BTW, I'm doing this in grails 1.1.1. I assume it shouldn't matter though.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
>

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: HQL on collections

pronto_mike
Maybe it would help if I showed the exception I was getting when I
tried the criteria query :)

This is what I got on the console:

Hibernate:
    select
        this_.id as id22_0_,
        this_.version as version22_0_,
        this_.aliases as aliases22_0_,
        this_.name as name22_0_,
        this_.state_id as state5_22_0_
    from
        school this_
    where
        this_.aliases like ?
2009-11-14 11:55:09,847 [236178619@qtp1-5] ERROR
errors.GrailsExceptionResolver  - groovy.lang.MissingMethodException:
No signature of method: static School.call() is applicable for
argument types: (TestController$_closure1_closure2_closure3_closure4)
values: [TestController$_closure1_closure2_closure3_closure4@2eb34620]
org.codehaus.groovy.runtime.InvokerInvocationException:
groovy.lang.MissingMethodException: No signature of method: static
School.call() is applicable for argument types:
(TestController$_closure1_closure2_closure3_closure4) values:
[TestController$_closure1_closure2_closure3_closure4@2eb34620]
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:92)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)


-Mike

On Sat, Nov 14, 2009 at 11:53 AM, Michael Masters <[hidden email]> wrote:

> No luck :(
>
> I just realized my description was a little confusing. I was using
> Course and Class interchangeably. Let me be a little more explicit
> about the domain objects.
>
> class Instructor {
>    String firstName
>    String lastName
>    String middleInitial
>
>    static hasMany = [ courses:Course ]
> }
>
> class Course {
>    School school
>    static belongsTo = [ instructor:Instructor ]
> }
>
> class School {
>    String name
>    State state
>    String aliases
> }
>
> Thanks,
> Mike
>
>
>
>
>
>
> 2009/11/14 Nicolás Dijkstra <[hidden email]>:
>> have you tried with
>> Instructor.withCriteria {
>>     classes {
>>        school {
>>            idEq(schoolId)
>>        }
>>     }
>> }
>>
>> Hope it helps!
>>
>> On Sat, Nov 14, 2009 at 5:14 AM, Michael Masters <[hidden email]> wrote:
>>>
>>> I have the following Domain classes:
>>>
>>> Instructor
>>> Class
>>> School
>>>
>>> Instructors have a collection of Classes and a class is associated to a
>>> school.
>>>
>>> I need to be able to query for all instructors in a school. I've tried
>>> the following and it's not working:
>>>
>>> 'Select i from Instructor i, Course c where c.school  = :school and c
>>> in elements(i.courses)'
>>>
>>> I've tried other variations without any luck. Any suggestions?
>>>
>>> -Mike
>>>
>>> BTW, I'm doing this in grails 1.1.1. I assume it shouldn't matter though.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>>
>

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: HQL on collections

pronto_mike
I'm not sure if this is even correct, but I tried the following and I
get an exception:

        def instructors = Instructor.withCriteria {
                                courses {
                                    yearEq(2002)
                                }
                            }


On Sat, Nov 14, 2009 at 11:57 AM, Michael Masters <[hidden email]> wrote:

> Maybe it would help if I showed the exception I was getting when I
> tried the criteria query :)
>
> This is what I got on the console:
>
> Hibernate:
>    select
>        this_.id as id22_0_,
>        this_.version as version22_0_,
>        this_.aliases as aliases22_0_,
>        this_.name as name22_0_,
>        this_.state_id as state5_22_0_
>    from
>        school this_
>    where
>        this_.aliases like ?
> 2009-11-14 11:55:09,847 [236178619@qtp1-5] ERROR
> errors.GrailsExceptionResolver  - groovy.lang.MissingMethodException:
> No signature of method: static School.call() is applicable for
> argument types: (TestController$_closure1_closure2_closure3_closure4)
> values: [TestController$_closure1_closure2_closure3_closure4@2eb34620]
> org.codehaus.groovy.runtime.InvokerInvocationException:
> groovy.lang.MissingMethodException: No signature of method: static
> School.call() is applicable for argument types:
> (TestController$_closure1_closure2_closure3_closure4) values:
> [TestController$_closure1_closure2_closure3_closure4@2eb34620]
>        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:92)
>        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
>
>
> -Mike
>
> On Sat, Nov 14, 2009 at 11:53 AM, Michael Masters <[hidden email]> wrote:
>> No luck :(
>>
>> I just realized my description was a little confusing. I was using
>> Course and Class interchangeably. Let me be a little more explicit
>> about the domain objects.
>>
>> class Instructor {
>>    String firstName
>>    String lastName
>>    String middleInitial
>>
>>    static hasMany = [ courses:Course ]
>> }
>>
>> class Course {
>>    School school
>>    static belongsTo = [ instructor:Instructor ]
>> }
>>
>> class School {
>>    String name
>>    State state
>>    String aliases
>> }
>>
>> Thanks,
>> Mike
>>
>>
>>
>>
>>
>>
>> 2009/11/14 Nicolás Dijkstra <[hidden email]>:
>>> have you tried with
>>> Instructor.withCriteria {
>>>     classes {
>>>        school {
>>>            idEq(schoolId)
>>>        }
>>>     }
>>> }
>>>
>>> Hope it helps!
>>>
>>> On Sat, Nov 14, 2009 at 5:14 AM, Michael Masters <[hidden email]> wrote:
>>>>
>>>> I have the following Domain classes:
>>>>
>>>> Instructor
>>>> Class
>>>> School
>>>>
>>>> Instructors have a collection of Classes and a class is associated to a
>>>> school.
>>>>
>>>> I need to be able to query for all instructors in a school. I've tried
>>>> the following and it's not working:
>>>>
>>>> 'Select i from Instructor i, Course c where c.school  = :school and c
>>>> in elements(i.courses)'
>>>>
>>>> I've tried other variations without any luck. Any suggestions?
>>>>
>>>> -Mike
>>>>
>>>> BTW, I'm doing this in grails 1.1.1. I assume it shouldn't matter though.
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>    http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>>
>>
>

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: HQL on collections

pronto_mike
Ha!!! I just got this to work, although it seems to run a little slow.
I'm doing the following to make this work:


        def school = School.findByName('Stanford')

        def instructors = Instructor.withCriteria {
                                courses {
                                    eq('school', school)
                                }
                            }

Any ideas on how to make this run faster? I assume if I can do it in
one query instead of two, then it would be a little faster. I tried
the following, but it doesn't work:

        def instructors = Instructor.withCriteria {
                                courses {
                                    school {
                                        eq('name', 'Stanford')
                                    }
                                }
                            }

Any ideas?

-Mike


On Sat, Nov 14, 2009 at 12:17 PM, Michael Masters <[hidden email]> wrote:

> I'm not sure if this is even correct, but I tried the following and I
> get an exception:
>
>        def instructors = Instructor.withCriteria {
>                                courses {
>                                    yearEq(2002)
>                                }
>                            }
>
>
> On Sat, Nov 14, 2009 at 11:57 AM, Michael Masters <[hidden email]> wrote:
>> Maybe it would help if I showed the exception I was getting when I
>> tried the criteria query :)
>>
>> This is what I got on the console:
>>
>> Hibernate:
>>    select
>>        this_.id as id22_0_,
>>        this_.version as version22_0_,
>>        this_.aliases as aliases22_0_,
>>        this_.name as name22_0_,
>>        this_.state_id as state5_22_0_
>>    from
>>        school this_
>>    where
>>        this_.aliases like ?
>> 2009-11-14 11:55:09,847 [236178619@qtp1-5] ERROR
>> errors.GrailsExceptionResolver  - groovy.lang.MissingMethodException:
>> No signature of method: static School.call() is applicable for
>> argument types: (TestController$_closure1_closure2_closure3_closure4)
>> values: [TestController$_closure1_closure2_closure3_closure4@2eb34620]
>> org.codehaus.groovy.runtime.InvokerInvocationException:
>> groovy.lang.MissingMethodException: No signature of method: static
>> School.call() is applicable for argument types:
>> (TestController$_closure1_closure2_closure3_closure4) values:
>> [TestController$_closure1_closure2_closure3_closure4@2eb34620]
>>        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:92)
>>        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:234)
>>
>>
>> -Mike
>>
>> On Sat, Nov 14, 2009 at 11:53 AM, Michael Masters <[hidden email]> wrote:
>>> No luck :(
>>>
>>> I just realized my description was a little confusing. I was using
>>> Course and Class interchangeably. Let me be a little more explicit
>>> about the domain objects.
>>>
>>> class Instructor {
>>>    String firstName
>>>    String lastName
>>>    String middleInitial
>>>
>>>    static hasMany = [ courses:Course ]
>>> }
>>>
>>> class Course {
>>>    School school
>>>    static belongsTo = [ instructor:Instructor ]
>>> }
>>>
>>> class School {
>>>    String name
>>>    State state
>>>    String aliases
>>> }
>>>
>>> Thanks,
>>> Mike
>>>
>>>
>>>
>>>
>>>
>>>
>>> 2009/11/14 Nicolás Dijkstra <[hidden email]>:
>>>> have you tried with
>>>> Instructor.withCriteria {
>>>>     classes {
>>>>        school {
>>>>            idEq(schoolId)
>>>>        }
>>>>     }
>>>> }
>>>>
>>>> Hope it helps!
>>>>
>>>> On Sat, Nov 14, 2009 at 5:14 AM, Michael Masters <[hidden email]> wrote:
>>>>>
>>>>> I have the following Domain classes:
>>>>>
>>>>> Instructor
>>>>> Class
>>>>> School
>>>>>
>>>>> Instructors have a collection of Classes and a class is associated to a
>>>>> school.
>>>>>
>>>>> I need to be able to query for all instructors in a school. I've tried
>>>>> the following and it's not working:
>>>>>
>>>>> 'Select i from Instructor i, Course c where c.school  = :school and c
>>>>> in elements(i.courses)'
>>>>>
>>>>> I've tried other variations without any luck. Any suggestions?
>>>>>
>>>>> -Mike
>>>>>
>>>>> BTW, I'm doing this in grails 1.1.1. I assume it shouldn't matter though.
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>    http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>

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

    http://xircles.codehaus.org/manage_email