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

后端 未结 1 1000
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 15:36

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

class Dimension {

    String name

    static hasMany = [
        children: Dimension,
           


        
1条回答
  •  醉话见心
    2021-02-06 16:31

    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 getChildren()
      {
        AssignmentDependency.findAllByParent(this).child
      }
    
      Set 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.

    0 讨论(0)
提交回复
热议问题