Grails - 将多对多父母/孩子关系映射到单个连接表 [英] Grails - mapping a many-to-many parents/children relation to a single join table
问题描述
我的问题是基于以下(简化的)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 aDimensionDependency
is always the child. By specifyingall-delete-orphan
in the mapping, we make sure that when removing aparentDependency
from a child, the associatedDimensionDependency
is deleted from the database.You may also add convenience methods to your
Dimension
class to encapsulate operations onDimensionDependencies
, 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屋!