Grails - 将多对多父母/孩子关系映射到单个连接表 [英] Grails - mapping a many-to-many parents/children relation to a single join table

查看:103
本文介绍了Grails - 将多对多父母/孩子关系映射到单个连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是基于以下(简化的)Grails领域类

  class Dimension {

字符串名称

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


有没有办法将多对多父母/子女关系映射到单个连接表?

解决方案

据我所知,唯一的方法是创建另一个表示父子关系的域类

  class DimensionDependency {
父维度
维度子元素
$ b $ static属性=维度
}
$ b class Dimension {
static hasMany = [parentDependencies:DimensionDependency]
static mappedBy = [parentDependencies:'child']
static mapping = { parentDependencies cascade:'all-delete-orphan'}
}

mappedBy 关键字指定引用 DimensionDependency 的对象始终是子对象。通过在映射中指定 all-delete-orphan ,我们确保在从子项中移除 parentDependency 时,关联的 DimensionDependency 将从数据库中删除。



您也可以将便利方法添加到 Dimension 类来封装 DimensionDependencies 上的操作,以使界面更像GORM。

  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))
}
返回此
}

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

我一直在使用这种方法,并且迄今为止没有麻烦。


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?

解决方案

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.

这篇关于Grails - 将多对多父母/孩子关系映射到单个连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆