Grails Criteria Querying associations: filter on concrete Class

303 views
Skip to first unread message

Colin Harrington

unread,
Dec 29, 2009, 7:07:26 PM12/29/09
to groo...@googlegroups.com, us...@grails.codehaus.org
How do I use criteria to query associations of a specific type/subclass?

Account {
    static hasMany = [transactions: Transaction]
    ...
}

CreditAccount extends Account {
    ...
    String filterableProperty
}

class Transaction {
    static belongsTo = [account: Account]
    ...
    BigDecimal amount
}

How do I find transactions belonging to a CreditAccount (with a filterable property) ?

I want to do something like:

def criteria = Transaction.createCriteria()
criteria.list {
    accounts {
        eq("classOfAccount", CreditAccount) //This is wrong / How do I filter on the class?
        eq("filterableProperty", "foo")
    }
    ge('amount', 100)
}

I couldn't find anything about how to do this in the User Guide, Grails in Action or the Definitive Guide to Grails.  and I'm not sure I know where to look next.

Thank you,

Colin Harrington
colin.ha...@gmail.com
612.804.5472

Christopher Bartling

unread,
Dec 29, 2009, 8:08:57 PM12/29/09
to groo...@googlegroups.com
I'd try the Hibernate site.  

-- chris --

Sent from my iPhone
--
You received this message because you are subscribed to the "Groovy Users of Minnesota" group.
 
To post to this group, send email to groo...@googlegroups.com
To unsubscribe from this group, send email to groovymn-u...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/groovymn?hl=en

Joe

unread,
Dec 30, 2009, 10:47:38 AM12/30/09
to Groovy Users of Minnesota
You could also come at it from the other way. Instead of trying to
filter from the transaction create the critreria on the CreditAccount
object instead.


On Dec 29, 7:08 pm, Christopher Bartling <chris.bartl...@gmail.com>
wrote:


> I'd try the Hibernate site.
>
> -- chris --
>
> Sent from my iPhone
>

> On Dec 29, 2009, at 6:07 PM, Colin Harrington <colin.harring...@gmail.com

> > colin.harring...@gmail.com

Colin Harrington

unread,
Jan 3, 2010, 11:51:33 PM1/3/10
to us...@grails.codehaus.org, groo...@googlegroups.com
Thank you,

using eq("class", CreditAccount.name) works great.

Colin Harrington

On Tue, Dec 29, 2009 at 6:45 PM, Burt Beckwith <bu...@burtbeckwith.com> wrote:
The discriminator column is a String with the name 'class', so it'd be

  eq("class", CreditAccount.name)

Burt

> How do I use criteria to query associations of a specific type/subclass?
>
> Account {
>     static hasMany = [transactions: Transaction]
>     ...
> }
>
> CreditAccount extends Account {
>     ...
>     String filterableProperty
> }
>
> class Transaction {
>     static belongsTo = [account: Account]
>     ...
>     BigDecimal amount
> }
>
> How do I find transactions belonging to a CreditAccount (with a filterable
> property) ?
>
> I want to do something like:
>
> def criteria = Transaction.createCriteria()
> criteria.list {
>     accounts {
>         eq("classOfAccount", CreditAccount) *//This is wrong / How do I
> filter on the class?*

>         eq("filterableProperty", "foo")
>     }
>     ge('amount', 100)
> }
>
> I couldn't find anything about how to do this in the User Guide, Grails in
> Action or the Definitive Guide to Grails.  and I'm not sure I know where to
> look next.
>
> Thank you,
>
> Colin Harrington
> colin.ha...@gmail.com
> 612.804.5472
>

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

   http://xircles.codehaus.org/manage_email



Reply all
Reply to author
Forward
0 new messages