Quantcast

Named queries inside a criteria with joins

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Named queries inside a criteria with joins

lmivan
Hi all,

I was wondering if it is possible to use a named query inside a criteria with joins. Let me use an example:

class User {
   ...
   String name
   Boolean isDeleted

   static namedQueries = {
      validUsers {
          eq 'isDeleted', false
       }
    }   
}

class Tweet {
   ...
   String text
   User user
}

Inside a controller or a service I can get all valid users or all the valid users with name "john"

User.validUsers.list()
User.validUsers.findAllByName("John")

But, what if I want to get all tweets of valid users?. I would like to do something like:

def tweets = Tweet.createCriteria().list() {
   user.validUsers

   // or maybe
   user.validUsers()

   // or
   user {
      validUsers()
   }

But none of this options seems to work. Is possible to use a named query like that? or I need to do something like:

def tweets = Tweet.createCriteria().list() {
   user {
      eq 'isDeleted', false
   }
}

I want to write my "validUser" constraint only in one place and use it all over my application.

Thanks and regards, Iván.

--
El Blog de Iván López - http://lopezivan.blogspot.com
Twitter - http://www.twitter.com/ilopmar


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Named queries inside a criteria with joins

Jeff Brown-3

On Aug 8, 2012, at 8:39 AM, lmivan wrote:

> Hi all,
>
> I was wondering if it is possible to use a named query inside a criteria with joins. Let me use an example:
>
> class User {
>    ...
>    String name
>    Boolean isDeleted
>
>    static namedQueries = {
>       validUsers {
>           eq 'isDeleted', false
>        }
>     }  
> }
>
> class Tweet {
>    ...
>    String text
>    User user
> }
>
> Inside a controller or a service I can get all valid users or all the valid users with name "john"
>
> User.validUsers.list()
> User.validUsers.findAllByName("John")
>
> But, what if I want to get all tweets of valid users?. I would like to do something like:
>
> def tweets = Tweet.createCriteria().list() {
>    user.validUsers
>
>    // or maybe
>    user.validUsers()
>
>    // or
>    user {
>       validUsers()
>    }
> }
>
> But none of this options seems to work. Is possible to use a named query like that? or I need to do something like:
>
> def tweets = Tweet.createCriteria().list() {
>    user {
>       eq 'isDeleted', false
>    }
> }
>
> I want to write my "validUser" constraint only in one place and use it all over my application.
>
> Thanks and regards, Iván.


There is currently support for invoking named queries from named queries but only within a class, not across classes.  If you would like to file a JIRA requesting the new feature we can take a look at that.  The biggest issue will probably be coming up with a syntax that is backward compatible and sensible.

Thanks for your help.



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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Named queries inside a criteria with joins

lmivan
Thanks for your reply Jeff.


Regards, Iván.
--
El Blog de Iván López - http://lopezivan.blogspot.com
Twitter - http://www.twitter.com/ilopmar



On Thu, Aug 9, 2012 at 11:40 PM, Jeff Brown <[hidden email]> wrote:

On Aug 8, 2012, at 8:39 AM, lmivan wrote:

> Hi all,
>
> I was wondering if it is possible to use a named query inside a criteria with joins. Let me use an example:
>
> class User {
>    ...
>    String name
>    Boolean isDeleted
>
>    static namedQueries = {
>       validUsers {
>           eq 'isDeleted', false
>        }
>     }
> }
>
> class Tweet {
>    ...
>    String text
>    User user
> }
>
> Inside a controller or a service I can get all valid users or all the valid users with name "john"
>
> User.validUsers.list()
> User.validUsers.findAllByName("John")
>
> But, what if I want to get all tweets of valid users?. I would like to do something like:
>
> def tweets = Tweet.createCriteria().list() {
>    user.validUsers
>
>    // or maybe
>    user.validUsers()
>
>    // or
>    user {
>       validUsers()
>    }
> }
>
> But none of this options seems to work. Is possible to use a named query like that? or I need to do something like:
>
> def tweets = Tweet.createCriteria().list() {
>    user {
>       eq 'isDeleted', false
>    }
> }
>
> I want to write my "validUser" constraint only in one place and use it all over my application.
>
> Thanks and regards, Iván.


There is currently support for invoking named queries from named queries but only within a class, not across classes.  If you would like to file a JIRA requesting the new feature we can take a look at that.  The biggest issue will probably be coming up with a syntax that is backward compatible and sensible.

Thanks for your help.



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



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Named queries inside a criteria with joins

Jeff Brown-3
In reply to this post by Jeff Brown-3

On Aug 9, 2012, at 4:40 PM, Jeff Brown wrote:

>
> On Aug 8, 2012, at 8:39 AM, lmivan wrote:
>
>> Hi all,
>>
>> I was wondering if it is possible to use a named query inside a criteria with joins. Let me use an example:
>>
>> class User {
>>   ...
>>   String name
>>   Boolean isDeleted
>>
>>   static namedQueries = {
>>      validUsers {
>>          eq 'isDeleted', false
>>       }
>>    }  
>> }
>>
>> class Tweet {
>>   ...
>>   String text
>>   User user
>> }
>>
>> Inside a controller or a service I can get all valid users or all the valid users with name "john"
>>
>> User.validUsers.list()
>> User.validUsers.findAllByName("John")
>>
>> But, what if I want to get all tweets of valid users?. I would like to do something like:
>>
>> def tweets = Tweet.createCriteria().list() {
>>   user.validUsers
>>
>>   // or maybe
>>   user.validUsers()
>>
>>   // or
>>   user {
>>      validUsers()
>>   }
>> }
>>
>> But none of this options seems to work. Is possible to use a named query like that? or I need to do something like:
>>
>> def tweets = Tweet.createCriteria().list() {
>>   user {
>>      eq 'isDeleted', false
>>   }
>> }
>>
>> I want to write my "validUser" constraint only in one place and use it all over my application.
>>
>> Thanks and regards, Iván.
>
>
> There is currently support for invoking named queries from named queries but only within a class, not across classes.  If you would like to file a JIRA requesting the new feature we can take a look at that.  The biggest issue will probably be coming up with a syntax that is backward compatible and sensible.
>
> Thanks for your help.
>
>

Shortly after sending this note I recalled adding support for this a while back (https://github.com/grails/grails-core/commit/9a798b330295d84bc28730b512e8505ebde45cee).

There is support for calling named queries in one class from a named query in another related class.  In your example above you are trying to do this from a normal criteria query, which will not work, but you can do it from a named query.

See the code I added to http://jira.grails.org/browse/GRAILS-9327.



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


Loading...