Question

I want to build a 'Entity' class that can both have childs and parents:

class Entity {
    static hasMany [childs: Entity, parents: Entity]
}

But I dont know how to map my data (should I use mappedBy?)

My test:

@Test
void testEntityCanHaveAChild() {
    Entityparent = new Entity()
    Entitychild = new Entity()

    parent.addToChilds(child)

    assert parent.childs
    assert parent.childs.size() == 1
    assert parent.childs.contains(child)

    assert child.parents
    assert child.parents.size() == 1
    assert child.parents.contains(parent)
}

So from Parent I wanna be able to gets all childs and from Child I wanna gets all parents

Was it helpful?

Solution 3

The only way I found to do it is by adding a many-to-many table between "both table" and to simulate the "auto-relation" like this:

class EntityBound {
    Entity parent
    Entity child 
}

class Entity {
    static hasMany [bounds: EntityBound]


    def childs() {
        EntityBound.findAllByParent(this).collect{ it.child }
    }

    def parents() {
        EntityBound.findAllByChild(this).collect{ it.parent }
    }

    Entity addChild(Entity entity){
        addToBounds(new EntityBound(parent:this, child:child))
        return this
    }
}

OTHER TIPS

You can't do it like that.

In a one to many association, the foreign key is put into the "one" part.

Now from your example I understand that if person x is child of person y, that doesn't mean that person y is parent to person x? person x can have parents: person z and person t?

I will discuss first the case when person x can have only one parent and if x is child of y, then y is parent to x.

For this case, I would add a new column, parent_id with a default value of null and say only that hasMany [children: Person] and belongsTo [parent: Person] This solve the case when one child can have only one parent.

For the other case, when one child can have multiple parents and not necessary being a child to someone makes that a parent for that child, are 2 options: 1. lists of ids in a db column (I do not like this in relational DB, but is acceptable in NoSQL) 2. connecting table(2). This can be one connecting table polymorphic, 2 connecting tables or one connecting tables with 3 columns and one will be always null. Then you need to add belongsTo to the owning part. And set the mapping for the join table manually. something like:

   static mapping = {
   hasMany joinTable: [name: 'parent_child_connection',
                       key: 'person_id',
                       column: 'parent_id']
}

Edit: Added addToChilds

What I would do is introduce another domain, e.g. ParentChild

class ParentChild {
    Person parent
    Person child 
}

then modify Person

class Person {
def parents() {
    ParentChild.executeQuery("select pc.parent from ParentChild pc where pc.child = :child", [child:this])
}

def children() {
    ParentChild.executeQuery("select pc.child from ParentChild pc where pc.parent = :parent", [parent:this])
}
def addToChilds(Person child){
    New ParentChild(parent:this, child:child).save()
}
}

Sorry I am only familiar with HQL. Not sure how to do this in more clean way

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top