Quantcast

How to implement multiple belongsTo's?

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

How to implement multiple belongsTo's?

MLoe
I have a domainclass address. Instances belong either to domainclass A or to domainclass B.

What is the best way to implement this.

class Address {
 ...
   static belongsTo = [a: A, b: B]

   static constraints = {
        a(nullable: true)
        b(nullable: true)
    }
}

doesn't work.

Should I use a Link Classes like this?

class AddressA {
   Address address
   static belongsTo = [a: A]
}
class AddressB {
   Address address
   static belongsTo = [b: B]
}

Or is there a better solution?

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

Re: How to implement multiple belongsTo's?

Alexandre Jacques
Michael,

If I understand you correctly, "mappedBy" should do the trick:

http://grails.org/doc/2.0.x/ref/Domain%20Classes/mappedBy.html

Regards,

Alexandre Jacques

On Tue, Mar 13, 2012 at 1:28 PM, MLoe <[hidden email]> wrote:

> I have a domainclass address. Instances belong either to domainclass A or to
> domainclass B.
>
> What is the best way to implement this.
>
> class Address {
>  ...
>   static belongsTo = [a: A, b: B]
>
>   static constraints = {
>        a(nullable: true)
>        b(nullable: true)
>    }
> }
>
> doesn't work.
>
> Should I use a Link Classes like this?
>
> class AddressA {
>   Address address
>   static belongsTo = [a: A]
> }
> class AddressB {
>   Address address
>   static belongsTo = [b: B]
> }
>
> Or is there a better solution?
>
> Thanks, Michael
>
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/How-to-implement-multiple-belongsTo-s-tp4469520p4469520.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
|  
Report Content as Inappropriate
star

Re: How to implement multiple belongsTo's?

Karsten Silz
In reply to this post by MLoe
Hi,

I have a domain class with a  "static belongsTo = [a: A, b: B]" statement - and it works for me. I have both "parents" set to "nullable: false, editable: false, display: false" in the constraints, too.
Loading...