GORM find item backwards in collection

View: New views
5 Messages — Rating Filter:   Alert me  

GORM find item backwards in collection

by Ted Vinke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!

I've got a GORM question. I've got an Ad with a list of Images which is
a one-way relationship. Now I need information BACKWARDS: I have an
Image and need to find the associated Ads with it.

My simplified Ad looks like:

        class Ad {

      List images
      static hasMany = [images:Image]
        }

Now I tried the following snippet:

        def image = Image.get( params.id )
        def ads = Ad.executeQuery( "select a.id from Ad a where :imageId in
a.images.id", [imageId:image.id] );

but this results in

        illegal attempt to dereference collection [ad0_.id.images] with element
property reference [id] [select a.id from Ad a where :imageId in
a.images.id];

I don't even know what that means. How to find the Ads which have the Image?

Any help is appreciated!
Kind regards,
Ted

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

    http://xircles.codehaus.org/manage_email



Re: GORM find item backwards in collection

by Konstantyn Smirnov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ted Vinke wrote:
def ads = Ad.executeQuery( "select a.id from Ad a where :imageId in
a.images.id", [imageId:image.id] );
try with

def ads = Ad.executeQuery( "select a from Ad a join a.images i where i.id=${params.id}" )

Re: GORM find item backwards in collection

by dahernan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

For this simple case the easy way is use bidirectional relationship.

http://grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.2.1.2%20One-to-many



2008/7/9 Konstantyn Smirnov <injecteer@...>:

>
>
> Ted Vinke wrote:
>>
>> def ads = Ad.executeQuery( "select a.id from Ad a where :imageId in
>> a.images.id", [imageId:image.id] );
>>
>
> try with
>
> def ads = Ad.executeQuery( "select a from Ad a join a.images i where
> i.id=${params.id}" )
> --
> View this message in context: http://www.nabble.com/GORM-find-item-backwards-in-collection-tp18357181p18358826.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



Re: GORM find item backwards in collection

by Konstantyn Smirnov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

dahernan wrote:
For this simple case the easy way is use bidirectional relationship.
unless Image is associated with objects, other than Ad :)

Re: GORM find item backwards in collection

by Ted Vinke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Konstantyn, not only your remark is in this case correct but your
earlier solutions to the problem too!

Regards,
Ted

Konstantyn Smirnov wrote:
>
>
> dahernan wrote:
>> For this simple case the easy way is use bidirectional relationship.
>>
>
> unless Image can be associated with other than Ad objects :)

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

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price