dynamic finders.

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

dynamic finders.

Mauro
If I have these two classes:

class GraUser {
    String firstName
    String lastName
    String email
    GraGroup group


String groupName

    static hasMany = [members: GraUser]

I can do GraUser.findByLastName("a"), or GraUser.findByEmail("b") but
I can't do GraUser.findByGroup("c").
I can only do GraGroup.findBygroupName("c").members.
Isn't it?

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: dynamic finders.

Scott Burch
However you can do:

GraUser.findByGroup(GraGroup.findByGroupName('c'))

The dynamic finders use the id of the object that you send to match up
the foreign key.  If you ask for something by GraGroup then you have to
give it a GraGroup object.



On Thu, 2010-01-07 at 00:05 +0100, Mauro wrote:

> If I have these two classes:
>
> class GraUser {
>     String firstName
>     String lastName
>     String email
>     GraGroup group
>
>
> String groupName
>
>     static hasMany = [members: GraUser]
>
> I can do GraUser.findByLastName("a"), or GraUser.findByEmail("b") but
> I can't do GraUser.findByGroup("c").
> I can only do GraGroup.findBygroupName("c").members.
> Isn't it?
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
--
SmartWeb Systems - "Websites that think, should be smart"
Groovy/Grails development
Phone (USA): 518-632-6679
Phone (CAN): 514-885-6457
http://www.smartwebsystems.biz



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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: dynamic finders.

Konstantyn Smirnov

nycsailor wrote
However you can do:

GraUser.findByGroup(GraGroup.findByGroupName('c'))
that would mean 2 selects rather than one.

To avoid it, use criteria or HQL
Reply | Threaded
Open this post in threaded view
|

Re: dynamic finders.

Scott Burch
Yes, I was making a point, not recommending this exact syntax. :)

I was trying to keep it simple.

On Thu, 2010-01-07 at 04:41 -0800, Konstantyn Smirnov wrote:

>
>
> nycsailor wrote:
> >
> > However you can do:
> >
> > GraUser.findByGroup(GraGroup.findByGroupName('c'))
> >
> that would mean 2 selects rather than one.
>
> To avoid it, use criteria or HQL



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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: dynamic finders.

Mauro
In reply to this post by Konstantyn Smirnov
2010/1/7 Konstantyn Smirnov <[hidden email]>:

>
>
>
> nycsailor wrote:
>>
>> However you can do:
>>
>> GraUser.findByGroup(GraGroup.findByGroupName('c'))
>>
> that would mean 2 selects rather than one.
>
> To avoid it, use criteria or HQL

something like this?

def dir = GraGroup.findByGroupName("c")
GraUser.createCriteria().list
  eq('group', dir)
}

>
> -----
> Konstantyn Smirnov, CTO
> http://www.poiradar.ru www.poiradar.ru
> http://www.poiradar.com.ua www.poiradar.com.ua
> http://www.poiradar.com www.poiradar.com
> http://www.poiradar.de www.poiradar.de
> --
> View this message in context: http://old.nabble.com/dynamic-finders.-tp27052365p27059361.html
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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: dynamic finders.

Nicolás Dijkstra
you can also do something like this

def users = GraUser.withCriteria {
    group {
        like('name', 'some group name')
    }
}

Regards

On Fri, Jan 8, 2010 at 4:32 PM, Mauro <[hidden email]> wrote:
2010/1/7 Konstantyn Smirnov <[hidden email]>:
>
>
>
> nycsailor wrote:
>>
>> However you can do:
>>
>> GraUser.findByGroup(GraGroup.findByGroupName('c'))
>>
> that would mean 2 selects rather than one.
>
> To avoid it, use criteria or HQL

something like this?

def dir = GraGroup.findByGroupName("c")
GraUser.createCriteria().list
 eq('group', dir)
}

>
> -----
> Konstantyn Smirnov, CTO
> http://www.poiradar.ru www.poiradar.ru
> http://www.poiradar.com.ua www.poiradar.com.ua
> http://www.poiradar.com www.poiradar.com
> http://www.poiradar.de www.poiradar.de
> --
> View this message in context: http://old.nabble.com/dynamic-finders.-tp27052365p27059361.html
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> 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: dynamic finders.

Mauro
2010/1/8 Nicolás Dijkstra <[hidden email]>:
> you can also do something like this
> def users = GraUser.withCriteria {
>     group {
>         like('name', 'some group name')
>     }
> }

This return a list, it is like createCriteria().list(), but if I want
a count like createCriteria().count()?
I was trying
def dir = GraGroup.findByGroupName("c")
GraUser.createCriteria().count(){
 eq('group', GraGroup.findByGroupNAme("dir")
}

But I think this is like GraUser.findByGroup(GraGroup.findByGroupName('c')).
I do two selects also with criteria version.

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: dynamic finders.

Konstantyn Smirnov

Mauro-27 wrote
I do two selects also with criteria version.
you're overcomplicating it a bit :)

GraUser.createCriteria().get{
  projections{
    rowCount()
  }
  group{
    eq 'name', 'dir'
  }
}

Here you have a unique result which is *count* and you have a single select
Reply | Threaded
Open this post in threaded view
|

Re: dynamic finders.

Jeff Scott Brown
On Fri, Jan 8, 2010 at 2:04 PM, Konstantyn Smirnov <[hidden email]> wrote:

>
>
>
> Mauro-27 wrote:
>>
>> I do two selects also with criteria version.
>>
> you're overcomplicating it a bit :)
>
> GraUser.createCriteria().get{
>  projections{
>    rowCount()
>  }
>  group{
>    eq 'name', 'dir'
>  }
> }
>
> Here you have a unique result which is *count* and you have a single select
>

Another option is something like this...

GraUser.createCriteria().count{
 group{
   eq 'name', 'dir'
 }
}

Also, if you are using Grails 1.2, look at the named criteria support.
 You can define a named criteria in your domain class, which may be
parameterized if necessary, and then invoke .count() on that.
Examples are available at
http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html.



jb
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email