Self-referencing one-to-many: how to get the owner?

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

Self-referencing one-to-many: how to get the owner?

by syg6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have this class:

class User
{
        String name
        static hasMany=[users:User]
}

A uni-directional one-to-many where each User can contain a Collection of Users, and each User belongs to exactly one User. This might seem a little complicated but it's what I need. The reason is I have different types of Users: (these aren't the real names ...)

User type I, BigBoss - has 1-3 Managers, belongs to no one.
User type II, Manager - has 1-5 Schmucks, belongs to a BigBoss.
User type III, Schmuck - has nobody, belongs to a Manager.

The problem is, I'd like to know who my 'owner' is. In other words, if I am a Schmuck, who is my Manager, and if I am a Manager, who is my BigBoss?

I tried making the following change to my class, to make it bi-directional:

class User
{
        String name
        User user

        static hasMany=[users:User]
        static belongsTo = User
}

But it doesn't work. Now my Collections aren't being populated.

Can someone help me out?

Thanks,
Bob

Re: Self-referencing one-to-many: how to get the owner?

by burtbeckwith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I just did something similar; I think the mapping would be:

   class User {
      String name

      static hasMany=[users:User]
      static belongsTo = [user: User]
      static constraints = {
         user nullable: true
      }
   }

'user' is nullable for the BigBoss case.

Burt

On Thursday 15 May 2008 10:50:30 am syg6 wrote:

> I have this class:
>
> class User
> {
> String name
> static hasMany=[users:User]
> }
>
> A uni-directional one-to-many where each User can contain a Collection of
> Users, and each User belongs to exactly one User. This might seem a little
> complicated but it's what I need. The reason is I have different types of
> Users: (these aren't the real names ...)
>
> User type I, BigBoss - has 1-3 Managers, belongs to no one.
> User type II, Manager - has 1-5 Schmucks, belongs to a BigBoss.
> User type III, Schmuck - has nobody, belongs to a Manager.
>
> The problem is, I'd like to know who my 'owner' is. In other words, if I am
> a Schmuck, who is my Manager, and if I am a Manager, who is my BigBoss?
>
> I tried making the following change to my class, to make it bi-directional:
>
> class User
> {
> String name
>         User user
>
> static hasMany=[users:User]
>         static belongsTo = User
> }
>
> But it doesn't work. Now my Collections aren't being populated.
>
> Can someone help me out?
>
> Thanks,
> Bob



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

    http://xircles.codehaus.org/manage_email



Re: Self-referencing one-to-many: how to get the owner?

by syg6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hmmm ... this is similar to what I tried. And gives the same result. :(

When I print out user.users or user.user it's blank. And there's data in the database. With only the 'hasMany' mapping user.users works fine.

Any other suggestions?

Bob


burtbeckwith wrote:
I just did something similar; I think the mapping would be:

   class User {
      String name

      static hasMany=[users:User]
      static belongsTo = [user: User]
      static constraints = {
         user nullable: true
      }
   }

'user' is nullable for the BigBoss case.

Burt

On Thursday 15 May 2008 10:50:30 am syg6 wrote:
> I have this class:
>
> class User
> {
> String name
> static hasMany=[users:User]
> }
>
> A uni-directional one-to-many where each User can contain a Collection of
> Users, and each User belongs to exactly one User. This might seem a little
> complicated but it's what I need. The reason is I have different types of
> Users: (these aren't the real names ...)
>
> User type I, BigBoss - has 1-3 Managers, belongs to no one.
> User type II, Manager - has 1-5 Schmucks, belongs to a BigBoss.
> User type III, Schmuck - has nobody, belongs to a Manager.
>
> The problem is, I'd like to know who my 'owner' is. In other words, if I am
> a Schmuck, who is my Manager, and if I am a Manager, who is my BigBoss?
>
> I tried making the following change to my class, to make it bi-directional:
>
> class User
> {
> String name
>         User user
>
> static hasMany=[users:User]
>         static belongsTo = User
> }
>
> But it doesn't work. Now my Collections aren't being populated.
>
> Can someone help me out?
>
> Thanks,
> Bob



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

    http://xircles.codehaus.org/manage_email


Re: Self-referencing one-to-many: how to get the owner?

by Joe Biron-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You need to find out what the error is when saving. Try printing out
the errors collection on your user object after calling save().


On 5/16/08, syg6 <syg6@...> wrote:

>
> Hmmm ... this is similar to what I tried. And gives the same result. :(
>
> When I print out user.users or user.user it's blank. And there's data in the
> database. With only the 'hasMany' mapping user.users works fine.
>
> Any other suggestions?
>
> Bob
>
>
>
> burtbeckwith wrote:
>>
>> I just did something similar; I think the mapping would be:
>>
>>    class User {
>>       String name
>>
>>       static hasMany=[users:User]
>>       static belongsTo = [user: User]
>>       static constraints = {
>>          user nullable: true
>>       }
>>    }
>>
>> 'user' is nullable for the BigBoss case.
>>
>> Burt
>>
>> On Thursday 15 May 2008 10:50:30 am syg6 wrote:
>>> I have this class:
>>>
>>> class User
>>> {
>>> String name
>>> static hasMany=[users:User]
>>> }
>>>
>>> A uni-directional one-to-many where each User can contain a Collection of
>>> Users, and each User belongs to exactly one User. This might seem a
>>> little
>>> complicated but it's what I need. The reason is I have different types of
>>> Users: (these aren't the real names ...)
>>>
>>> User type I, BigBoss - has 1-3 Managers, belongs to no one.
>>> User type II, Manager - has 1-5 Schmucks, belongs to a BigBoss.
>>> User type III, Schmuck - has nobody, belongs to a Manager.
>>>
>>> The problem is, I'd like to know who my 'owner' is. In other words, if I
>>> am
>>> a Schmuck, who is my Manager, and if I am a Manager, who is my BigBoss?
>>>
>>> I tried making the following change to my class, to make it
>>> bi-directional:
>>>
>>> class User
>>> {
>>> String name
>>>         User user
>>>
>>> static hasMany=[users:User]
>>>         static belongsTo = User
>>> }
>>>
>>> But it doesn't work. Now my Collections aren't being populated.
>>>
>>> Can someone help me out?
>>>
>>> Thanks,
>>> Bob
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>
> --
> View this message in context:
> http://www.nabble.com/Self-referencing-one-to-many%3A-how-to-get-the-owner--tp17255019p17268633.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
>
>
>


--
-=--===-----========-------------======================
http://www.bironology.org/

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

    http://xircles.codehaus.org/manage_email



Re: Self-referencing one-to-many: how to get the owner?

by syg6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey, it actually DOES work! Sorry about the confusion. I hadn't noticed that Grails created a new column in my users table, 'user_id', and this it was using this new column to map the associations. I had put values in the old JOIN table, but it seems with this new mapping the JOIN table is no longer needed!

Many thanks!

Bob

Joe Biron-2 wrote:
You need to find out what the error is when saving. Try printing out
the errors collection on your user object after calling save().


On 5/16/08, syg6 <syg6@yahoo.com> wrote:
>
> Hmmm ... this is similar to what I tried. And gives the same result. :(
>
> When I print out user.users or user.user it's blank. And there's data in the
> database. With only the 'hasMany' mapping user.users works fine.
>
> Any other suggestions?
>
> Bob
>
>
>
> burtbeckwith wrote:
>>
>> I just did something similar; I think the mapping would be:
>>
>>    class User {
>>       String name
>>
>>       static hasMany=[users:User]
>>       static belongsTo = [user: User]
>>       static constraints = {
>>          user nullable: true
>>       }
>>    }
>>
>> 'user' is nullable for the BigBoss case.
>>
>> Burt
>>
>> On Thursday 15 May 2008 10:50:30 am syg6 wrote:
>>> I have this class:
>>>
>>> class User
>>> {
>>> String name
>>> static hasMany=[users:User]
>>> }
>>>
>>> A uni-directional one-to-many where each User can contain a Collection of
>>> Users, and each User belongs to exactly one User. This might seem a
>>> little
>>> complicated but it's what I need. The reason is I have different types of
>>> Users: (these aren't the real names ...)
>>>
>>> User type I, BigBoss - has 1-3 Managers, belongs to no one.
>>> User type II, Manager - has 1-5 Schmucks, belongs to a BigBoss.
>>> User type III, Schmuck - has nobody, belongs to a Manager.
>>>
>>> The problem is, I'd like to know who my 'owner' is. In other words, if I
>>> am
>>> a Schmuck, who is my Manager, and if I am a Manager, who is my BigBoss?
>>>
>>> I tried making the following change to my class, to make it
>>> bi-directional:
>>>
>>> class User
>>> {
>>> String name
>>>         User user
>>>
>>> static hasMany=[users:User]
>>>         static belongsTo = User
>>> }
>>>
>>> But it doesn't work. Now my Collections aren't being populated.
>>>
>>> Can someone help me out?
>>>
>>> Thanks,
>>> Bob
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>
> --
> View this message in context:
> http://www.nabble.com/Self-referencing-one-to-many%3A-how-to-get-the-owner--tp17255019p17268633.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
>
>
>


--
-=--===-----========-------------======================
http://www.bironology.org/

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

    http://xircles.codehaus.org/manage_email