Getting a unique result using named queries in 1.3.4

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

Getting a unique result using named queries in 1.3.4

scryan
I am trying to get named queries to return a single result rather than a list and have tried many of the tricks suggested on the list with no luck  I have the following named query that always only returns one result

class MailMessageTag
{
    /** If set to true this is a globally available tag.          */
    Boolean global = false
    /** This is the name of the tag which is unique by creator.          */
    String name
    /** The named queries for this object type     */
    static namedQueries =
    {
        sentTag
        {
            eq("global", true)
            eq("name", "sent")
        }
    }
}
I tried calling it with

MailMessageTag.sentTag.get()

which returns a null which might be expected since I can place a number in the get method and get only that object reference.  So how do i get one result rather than a list?

For a normal criteria query I can use the get function to return only one value

Thanks

Scott Ryan


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Getting a unique result using named queries in 1.3.4

Dana
  Try this:

MailMessageTag.sentTag.list(max: 1)

or if you want the item not in a list:

MailMessageTag.sentTag.list(max: 1)?.first()

-Dana

On 9/10/10 2:07 PM, Scott Ryan wrote:

> I am trying to get named queries to return a single result rather than a list and have tried many of the tricks suggested on the list with no luck  I have the following named query that always only returns one result
>
> class MailMessageTag
> {
>      /** If set to true this is a globally available tag.          */
>      Boolean global = false
>      /** This is the name of the tag which is unique by creator.          */
>      String name
>      /** The named queries for this object type     */
>      static namedQueries =
>      {
>          sentTag
>          {
>              eq("global", true)
>              eq("name", "sent")
>          }
>      }
> }
> I tried calling it with
>
> MailMessageTag.sentTag.get()
>
> which returns a null which might be expected since I can place a number in the get method and get only that object reference.  So how do i get one result rather than a list?
>
> For a normal criteria query I can use the get function to return only one value
>
> Thanks
>
> Scott Ryan
>
>
> ---------------------------------------------------------------------
> 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: Getting a unique result using named queries in 1.3.4

Brian Schmoll
In reply to this post by scryan
I had this same issue awhile back.  Try this thread, which might be what you're looking for:

http://grails.1312388.n4.nabble.com/Named-Queries-Return-a-Single-Object-td1569128.html

Brian

On Fri, Sep 10, 2010 at 4:07 PM, Scott Ryan <[hidden email]> wrote:
I am trying to get named queries to return a single result rather than a list and have tried many of the tricks suggested on the list with no luck  I have the following named query that always only returns one result

class MailMessageTag
{
   /** If set to true this is a globally available tag.          */
   Boolean global = false
   /** This is the name of the tag which is unique by creator.          */
   String name
   /** The named queries for this object type     */
   static namedQueries =
   {
       sentTag
       {
           eq("global", true)
           eq("name", "sent")
       }
   }
}
I tried calling it with

MailMessageTag.sentTag.get()

which returns a null which might be expected since I can place a number in the get method and get only that object reference.  So how do i get one result rather than a list?

For a normal criteria query I can use the get function to return only one value

Thanks

Scott Ryan


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

   http://xircles.codehaus.org/manage_email