Grails - mapping a many-to-many parents/children relation to a single join table

痴心易碎 提交于 2019-12-03 00:45:22

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!