Grails - mapping a many-to-many parents/children r

2020-05-29 04:43发布

问题:

My question is based on the following (simplified) Grails domain class

class Dimension {

    String name

    static hasMany = [
        children: Dimension,
        parents: Dimension
    ]
}

Is there a way to map the many-to-many parents/children relationship to a single join table?

回答1:

As far as I know, the only way to do that is to create another domain class that represents a parent-child relationship.

class DimensionDependency {
  Dimension parent
  Dimension child

  static belongsTo = Dimension
}

class Dimension {
  static hasMany = [parentDependencies: DimensionDependency]
  static mappedBy = [parentDependencies: 'child']
  static mapping = { parentDependencies cascade: 'all-delete-orphan' }
}

The mappedBy keywords specifies that the object refering to a DimensionDependency is always the child. By specifying all-delete-orphan in the mapping, we make sure that when removing a parentDependency from a child, the associated DimensionDependency is deleted from the database.

You may also add convenience methods to your Dimension class to encapsulate operations on DimensionDependencies, to make the interface more GORM-like.

  static transients = ['children', 'parents']

  Set<Dimension> getChildren()
  {
    AssignmentDependency.findAllByParent(this).child
  }

  Set<Dimension> getParents()
  { 
    parentDependencies?.parent ?: []
  }

  Dimension addToParents(Dimension parent)
  {
    if (!parentDependencies.find { it.parent == parent && it.child == this })
    {
      addToParentDependencies(new DimensionDependency(parent: parent, child: this))
    }
    return this
  }

  Dimension removeFromParents(Dimension parent)
  {
    def dep = parentDependencies.find { it.parent == parent }
    removeFromParentDependencies(dep)
    dep.delete(flush: true)
    return this
  }

I've been using this approach for some time and have had no trouble so far.