Quantcast

Join two entities to a third on a single column - bug ?

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

Join two entities to a third on a single column - bug ?

SN

Lets say I have Person, SimplePerson and Address

class Address {
static belongsTo = [person : Person]
}

class Person {
 static hasMany = [addresses: Address]
}
 
Now I have another view of the Person called SimplePerson (points to same table but has less column)  

class SimplePerson {
 static hasMany = [addresses:Address]

  static mapping = { 
 table 'person' 
 addresses column:'person_id'  
 }

}


So now I have mapped both Person and SimplePerson to Address with column 'person_id'
However this works for Person, but when SimpleOrganization is queried, the Adresses are always null, (though it does not through any error)

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

Re: Join two entities to a third on a single column - bug ?

burtbeckwith
It doesn't look like this is possible. You have a belongsTo in Address using map syntax, so you don't get a join table - the address table has a person_id foreign key. But when you add the hasMany to SimplePerson without a belongsTo, you get a join table which will always be empty since storing the person/address doesn't know about it.

I played with this a bit and added an extra belongsTo:

   static belongsTo = [person : Person, simplePerson: SimplePerson]

which eliminated the join table (needed to remove "addresses column:'person_id'" since that's no longer relevant) but then I got a simple_person_id column. Trying to fix that with a mapping worked for the DDL generation (run 'grails schema-export' to see it):

   static mapping = {
      simplePerson column: 'person_id'  
   }

but then at runtime I got the expected Hibernate exception "org.hibernate.MappingException: Repeated column in mapping for entity: Address column: person_id"

I wouldn't call this a bug though. It's a nice trick that you can map multiple domain classes to a table and create a view-like sub-domain, but given how collections are mapped it doesn't seem like they're compatible with this approach.

Burt

>
> Lets say I have Person, SimplePerson and Address
>
> class Address {
> static belongsTo = [person : Person]
> }
>
> class Person {
>  static hasMany = [addresses: Address]
> }
>  
> Now I have another view of the Person called SimplePerson (points to same table but has less column)  
>
> class SimplePerson {
>  static hasMany = [addresses:Address]
>
>   static mapping = {
>   table 'person'
>   addresses column:'person_id'  
>   }
>
> }
>
>
> So now I have mapped both Person and SimplePerson to Address with column 'person_id'
> However this works for Person, but when SimpleOrganization is queried, the Adresses are always null, (though it does not through any error)
>
> potentially bug ?

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

    http://xircles.codehaus.org/manage_email


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

Re: Join two entities to a third on a single column - bug ?

SN

Hello Burt,

So is there any workaround, alternative to achieve the same thing ?, I believe, without getting an extra column simple_person_id, it may not be possible.
 
   
Sudhir 
 


From: Burt Beckwith <[hidden email]>
To: [hidden email]
Sent: Sunday, 7 August 2011 9:24 PM
Subject: Re: [grails-user] Join two entities to a third on a single column - bug ?

It doesn't look like this is possible. You have a belongsTo in Address using map syntax, so you don't get a join table - the address table has a person_id foreign key. But when you add the hasMany to SimplePerson without a belongsTo, you get a join table which will always be empty since storing the person/address doesn't know about it.

I played with this a bit and added an extra belongsTo:

  static belongsTo = [person : Person, simplePerson: SimplePerson]

which eliminated the join table (needed to remove "addresses column:'person_id'" since that's no longer relevant) but then I got a simple_person_id column. Trying to fix that with a mapping worked for the DDL generation (run 'grails schema-export' to see it):

  static mapping = {
      simplePerson column: 'person_id' 
  }

but then at runtime I got the expected Hibernate exception "org.hibernate.MappingException: Repeated column in mapping for entity: Address column: person_id"

I wouldn't call this a bug though. It's a nice trick that you can map multiple domain classes to a table and create a view-like sub-domain, but given how collections are mapped it doesn't seem like they're compatible with this approach.

Burt

>
> Lets say I have Person, SimplePerson and Address
>
> class Address {
> static belongsTo = [person : Person]
> }
>
> class Person {
>  static hasMany = [addresses: Address]
> }

> Now I have another view of the Person called SimplePerson (points to same table but has less column) 
>
> class SimplePerson {
>  static hasMany = [addresses:Address]
>
>  static mapping = {
>  table 'person'
>  addresses column:'person_id' 
>  }
>
> }
>
>
> So now I have mapped both Person and SimplePerson to Address with column 'person_id'
> However this works for Person, but when SimpleOrganization is queried, the Adresses are always null, (though it does not through any error)
>
> potentially bug ?

---------------------------------------------------------------------
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: Join two entities to a third on a single column - bug ?

burtbeckwith
As I said, I don't think it's possible. But I may be missing something.

Burt

>
> Hello Burt,
>
> So is there any workaround, alternative to achieve the same thing ?, I believe, without getting an extra column simple_person_id, it may not be possible.
>  
>    
> Sudhir
>  
>
>
> ________________________________
> From: Burt Beckwith <[hidden email]>
> To: [hidden email]
> Sent: Sunday, 7 August 2011 9:24 PM
> Subject: Re: [grails-user] Join two entities to a  third  on a single column - bug ?
>
> It doesn't look like this is possible. You have a belongsTo in Address using map syntax, so you don't get a join table - the address table has a person_id foreign key. But when you add the hasMany to SimplePerson without a belongsTo, you get a join table which will always be empty since storing the person/address doesn't know about it.
>
> I played with this a bit and added an extra belongsTo:
>
>    static belongsTo = [person : Person, simplePerson: SimplePerson]
>
> which eliminated the join table (needed to remove "addresses column:'person_id'" since that's no longer relevant) but then I got a simple_person_id column. Trying to fix that with a mapping worked for the DDL generation (run 'grails schema-export' to see it):
>
>    static mapping = {
>       simplePerson column: 'person_id'  
>    }
>
> but then at runtime I got the expected Hibernate exception "org.hibernate.MappingException: Repeated column in mapping for entity: Address column: person_id"
>
> I wouldn't call this a bug though. It's a nice trick that you can map multiple domain classes to a table and create a view-like sub-domain, but given how collections are mapped it doesn't seem like they're compatible with this approach.
>
> Burt
>
> >
> > Lets say I have Person, SimplePerson and Address
> >
> > class Address {
> > static belongsTo = [person : Person]
> > }
> >
> > class Person {
> >  static hasMany = [addresses: Address]
> > }
> >  
> > Now I have another view of the Person called SimplePerson (points to same table but has less column)  
> >
> > class SimplePerson {
> >  static hasMany = [addresses:Address]
> >
> >   static mapping = {
> >   table 'person'
> >   addresses column:'person_id'  
> >   }
> >
> > }
> >
> >
> > So now I have mapped both Person and SimplePerson to Address with column 'person_id'
> > However this works for Person, but when SimpleOrganization is queried, the Adresses are always null, (though it does not through any error)
> >
> > potentially bug ?
>
> ---------------------------------------------------------------------
> 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


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

Re: Join two entities to a third on a single column - bug ?

SN
okay, Thanks for the quick response.
   
Sudhir 
 


From: Burt Beckwith <[hidden email]>
To: [hidden email]
Sent: Tuesday, 9 August 2011 12:11 PM
Subject: Re: [grails-user] Join two entities to a third on a single column - bug ?

As I said, I don't think it's possible. But I may be missing something.

Burt

>
> Hello Burt,
>
> So is there any workaround, alternative to achieve the same thing ?, I believe, without getting an extra column simple_person_id, it may not be possible.

>   
> Sudhir

>
>
> ________________________________
> From: Burt Beckwith <[hidden email]>
> To: [hidden email]
> Sent: Sunday, 7 August 2011 9:24 PM
> Subject: Re: [grails-user] Join two entities to a  third  on a single column - bug ?
>
> It doesn't look like this is possible. You have a belongsTo in Address using map syntax, so you don't get a join table - the address table has a person_id foreign key. But when you add the hasMany to SimplePerson without a belongsTo, you get a join table which will always be empty since storing the person/address doesn't know about it.
>
> I played with this a bit and added an extra belongsTo:
>
>    static belongsTo = [person : Person, simplePerson: SimplePerson]
>
> which eliminated the join table (needed to remove "addresses column:'person_id'" since that's no longer relevant) but then I got a simple_person_id column. Trying to fix that with a mapping worked for the DDL generation (run 'grails schema-export' to see it):
>
>    static mapping = {
>      simplePerson column: 'person_id' 
>    }
>
> but then at runtime I got the expected Hibernate exception "org.hibernate.MappingException: Repeated column in mapping for entity: Address column: person_id"
>
> I wouldn't call this a bug though. It's a nice trick that you can map multiple domain classes to a table and create a view-like sub-domain, but given how collections are mapped it doesn't seem like they're compatible with this approach.
>
> Burt
>
> >
> > Lets say I have Person, SimplePerson and Address
> >
> > class Address {
> > static belongsTo = [person : Person]
> > }
> >
> > class Person {
> >  static hasMany = [addresses: Address]
> > }
> > 
> > Now I have another view of the Person called SimplePerson (points to same table but has less column) 
> >
> > class SimplePerson {
> >  static hasMany = [addresses:Address]
> >
> >  static mapping = {
> >  table 'person'
> >  addresses column:'person_id' 
> >  }
> >
> > }
> >
> >
> > So now I have mapped both Person and SimplePerson to Address with column 'person_id'
> > However this works for Person, but when SimpleOrganization is queried, the Adresses are always null, (though it does not through any error)
> >
> > potentially bug ?
>
> ---------------------------------------------------------------------
> 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: Join two entities to a third on a single column - bug ?

jphiloon
This is probably way off the mark, but with a similar problem where I wanted both a quick and a detailed view of the same object - your Person, in this case -  I make two domain classes pointing to the same table.  One is the quick view (with addresses in your case), the other is the detailed view with many other Person properties.   I then reference the detailed view from the quick view.  Because of lazy loading, the detailed properties only get loaded if they are actually accessed.  In my case the detailed properties were clobs which I did not want to load unless I really wanted them.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Join two entities to a third on a single column - bug ?

burtbeckwith
That's exactly what he did. The issue is that collections are handled differently from regular column-based properties.

Burt

> This is probably way off the mark, but with a similar problem where I wanted
> both a quick and a detailed view of the same object - your Person, in this
> case -  I make two domain classes pointing to the same table.  One is the
> quick view (with addresses in your case), the other is the detailed view
> with many other Person properties.   I then reference the detailed view from
> the quick view.  Because of lazy loading, the detailed properties only get
> loaded if they are actually accessed.  In my case the detailed properties
> were clobs which I did not want to load unless I really wanted them.
>
> --
> View this message in context: http://grails.1312388.n4.nabble.com/Join-two-entities-to-a-third-on-a-single-column-bug-tp3723019p3729808.html

---------------------------------------------------------------------
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: Join two entities to a third on a single column - bug ?

jphiloon
Hmmm.  I was thinking that if the address association was only in one Person class that might take care of this particular problem.  I know it's a work-around - not a general solution in terms of inheritance or composition, involves a lazy fetch by default, etc.  

class Address {
    static belongsTo = [person : Person]
}

class Person {
    static hasMany = [addresses: Address]
    ComplicatedPerson complicatedPerson
}

class ComplicatedPerson {
  String phobias
}
Loading...