0
votes

I am having trouble grasping the concept of a belongsTo - hasMany relationship in Grails when the child class could belong to one of two properties in the parent (but not both).

For instance:

class Store {
    Integer storeNumber
    List employees
    List managers

    static hasMany = [employees:Person,
                      managers:Person]
}

class Person {
    String name
    String roll

    static belongsTo = [store:Store]

    static constraints = {
        role inList: ['employee','manager']
    }
}

Where Person is in either the list of Store.employees OR the list of Store.managers

I am getting an error on Person having "Repeated column in mapping...". I tried a few static mapping attempts, but still not understanding how to do it properly.

How do I properly map this?

Thanks in advance....

1

1 Answers

1
votes
class Store {
    Integer storeNumber
    //List employees
    //List managers

    static hasMany = [employees:Person,
                      managers:Person]
    static constraints = {
      employees(nullable:true)
      managers(nullable:true)
    }
}

class Person {
    String name
    String roll

    static belongsTo = [store:Store]

    static constraints = {
        role inList: ['employee','manager']
    }
}

Now add a store

Store store = new Store(storeNumber: 1).save()
if (store) {
  Person person = new Person(name:'fred', roll:'employee', store:store).save()
  if (person) {
    store.addToEmployees(person)
  }
}

So the gain obviously with the headache was a quick way of reverse lookingup parent, alternative is the loose relation described below which doesn't care about parent so then you don't need this last bit and obviously loose the dependancy issues that you hit so far

End E2A

You need one to have the other relationship

Alternatively if you didn't care about the belongsTo relationship then things would be easier like

static belongsTo = Store

Then that factor is no longer an issue.

The difference is with the current method you have vs the latter I shown you - you can reverse walk back to parent easily from a child if query was started from child

either way you could always start query from parent then join child and look for child aspect

so what suites you sir

Final Edit You know when you been doing this stuff for years you find ways of doing things in alternative ways:

Lets assume you did set up a loose/weak reference from Parent to Child and you wanted to get access to Parent. Here is how:

Class User {
     String username
    static hasMany=[childs:Children]
}

Class Children {
    String firstName
    static belongsTo=User

   // now to get access back to parent user without knowing parent a hackish thing like this can be done:

    User getUser() {
        User.findByChilds(this)
    }
    String getUserName() {
        return user?.username
    }

}

Now from a child you could do ${instance.user} or ${instance.userName} this will then bind back to parent and find by child object itself findByChilds(this) Years ago this would have I guess warped me