本文介绍了在一个Grails域类中多个多对多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Grails 3.0.6,并且正在与一个复杂且高度互联的领域模型斗争。我有类与多个多对多的关联到其他类,我没有选择,只能在至少一个类上有多个belongsTo关联。我无法找出表示这种情况的语法。



我的域模型非常复杂,但我能够将我的问题简化为这个简化的示例:

  class图{
static hasMany = [vertices:Vertex]
}

class OtherClass {
static hasMany = [vertices:Vertex]
}

class Vertex {
static hasMany = [graph:Graph,other:OtherClass]
}在这个简化的例子中,我可以通过声明域类之间的所有权Graph和OtherClass ...在我复杂的领域模型中,我没有这个选择,因为有太多的类有多个多对多的关联。



I已经尝试过:

$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b静态belongsTo =图形,其他类
}

但是我得到了一个N​​PE。 / p>

我试过这个:

  class Vertex {
static hasMany = [graph:Graph,others:OtherClass]
static belongsTo = [graph:Graph,others:OtherClass]
}

但我仍然得到GrailsDomainException:没有在域类[Graph]和[Vertex]之间定义所有者

我可以用mappedBy来正确表示这个吗?

在许多多对多关联中,级联保存并不是真正需要的(尽管它们不会受到伤害),所以我不需要belongsTo(或所有者)。这让我想知道域类的关联是否真的应该如何建模这些关系。还有什么我可以做的吗?

解决方案

根据Burt Beckwith的评论,我创建了一个额外的域类来表示连接表。现在,一个多对多的关联被分解为两个一对多的关联,并且问题不会出现。



示例:

  class Graph {
static hasMany = [graphVertexRelations:GraphVertexRelation]
}

class OtherClass {
static hasMany = [vertices:Vertex]
}

class Vertex {
static hasMany = [graphVertexRelations:GraphVertexRelation,others:OtherClass]
static belongsTo = OtherClass
}

class GraphVertexRelation {
static belongsTo = [graph:Graph,vertex:Vertex]

static GraphVertexRelation create(Graph graph,Vertex vertex ,boolean flush = false){
new GraphVertexRelation(graph:graph,vertex:vertex).save(flush:flush,insert:true)
}
}


I am using Grails 3.0.6 and am struggling with a complicated and highly interconnected domain model. I have classes with multiple many-to-many associations to other classes and I am left with no choice but to have multiple belongsTo associations on at least one class. I am unable to figure out the syntax to represent this.

My domain model was quite complicated, but I was able to reduce my problem to this simplified example:

class Graph {
    static hasMany = [vertices: Vertex]
}

class OtherClass {
    static hasMany = [vertices: Vertex]
}

class Vertex {
    static hasMany = [graph: Graph, other: OtherClass]
}

In this simplified example, I could get around the problem by declaring the ownership between the domain classes on Graph and OtherClass... In my complicated domain model, I don't have this choice because there are too many classes with multiple many-to-many associations.

I have tried this:

class Vertex {
    static hasMany = [graphs: Graph, others: OtherClass]
    static belongsTo = Graph, OtherClass
}

but I get an NPE.

I have tried this:

class Vertex {
    static hasMany = [graphs: Graph, others: OtherClass]
    static belongsTo = [graphs: Graph, others: OtherClass]
}

but I still get "GrailsDomainException: No owner defined between domain classes [Graph] and [Vertex]"

Is there something I could do with mappedBy to correctly represent this?

In many of my many-to-many associations, cascading saves are not actually wanted (although they won't hurt), so I don't need belongsTo (or an "owner") for that purpose. This makes me wonder if associations on the domain classes are really how I should be modeling these relationships. Is there something else I could be doing?

解决方案

per Burt Beckwith's comment, I created an additional domain class to represent the join table. Now, one many-to-many association is broken down into two one-to-many associations and the problem does not arise.

Example:

class Graph {
    static hasMany = [graphVertexRelations: GraphVertexRelation]
}

class OtherClass {
    static hasMany = [vertices: Vertex]
}

class Vertex {
    static hasMany = [graphVertexRelations: GraphVertexRelation, others: OtherClass]
    static belongsTo = OtherClass
}

class GraphVertexRelation {
    static belongsTo = [graph: Graph, vertex: Vertex]

    static GraphVertexRelation create(Graph graph, Vertex vertex, boolean flush = false) {
        new GraphVertexRelation(graph: graph, vertex: vertex).save(flush: flush, insert: true)
    }
}

这篇关于在一个Grails域类中多个多对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:17