一个Grails域类中的多个多对多关联

一个Grails域类中的多个多对多关联,grails,gorm,grails-3.0,Grails,Gorm,Grails 3.0,我正在使用Grails3.0.6,并且正在努力处理一个复杂且高度互联的领域模型。我的课程与其他课程有多个多对多的关联,我别无选择,只能在至少一个课程上有多个belongsTo关联。我无法理解表示这一点的语法 我的域模型相当复杂,但我能够将问题简化为以下简化示例: class Graph { static hasMany = [vertices: Vertex] } class OtherClass { static hasMany = [vertices: Vertex] }

我正在使用Grails3.0.6,并且正在努力处理一个复杂且高度互联的领域模型。我的课程与其他课程有多个多对多的关联,我别无选择,只能在至少一个课程上有多个belongsTo关联。我无法理解表示这一点的语法

我的域模型相当复杂,但我能够将问题简化为以下简化示例:

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

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

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

我试过这个:

class Vertex {
    static hasMany = [graphs: Graph, others: OtherClass]
    static belongsTo = Graph, OtherClass
}
class Vertex {
    static hasMany = [graphs: Graph, others: OtherClass]
    static belongsTo = [graphs: Graph, others: OtherClass]
}
但我有一个NPE

我试过这个:

class Vertex {
    static hasMany = [graphs: Graph, others: OtherClass]
    static belongsTo = Graph, OtherClass
}
class Vertex {
    static hasMany = [graphs: Graph, others: OtherClass]
    static belongsTo = [graphs: Graph, others: OtherClass]
}
但我仍然得到“GrailDomainException:在域类[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)
    }
}

您看到的异常“GrailDomainException:域类[Graph]和[Vertex]之间没有定义所有者”意味着ORM无法确定基类是什么,Graph和Vertex之间存在循环关系

如果要维护关系以查看顶点所在的图形,可以使用条件进行反向查找

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

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

class Vertex {
    static transients = ['graphs']
    static hasMany = [other: OtherClass]

    List<Graph> getGraphs() {
        // Backwards link, using the graph table
        Graph.withCriteria() {
            vertices {
                inList("id", [this.id.toLong()])
            }
        }
    }
}
类图{
静态hasMany=[顶点:顶点]
}
另一类{
静态hasMany=[顶点:顶点]
}
类顶点{
静态瞬变=['图形']
static hasMany=[其他:其他类]
列表getGraphs(){
//反向链接,使用图表表
图.withCriteria(){
顶点{
inList(“id”,[this.id.toLong()]))
}
}
}
}

考虑使用域类显式映射联接表,如spring security core中的
UserRole
类。@Burtbeckw您正在谈论这个,对吗?我以前见过这样的事。如果我创建一个名为VertexGraph的类,它会映射到联接表吗?VertexGraph和GraphVertex有关系吗?对-这是一个大问题,不太方便,但您可以获得更多的控制(并且不会因为集合而出现性能问题)。使用此方法时,某些条件查询不起作用,但我可以帮助您处理任何特定的问题issues@BurtBeckwith我能够让我的玩具示例与您的建议一起工作——请参见下面的答案。你是这么想的吗?我真的需要多对多的关系。若我正确理解了瞬变,这就是在图和顶点之间创建一对多关系。我需要顶点能够在多个图中,好的。这是您的数据模型,您显然有未列出的约束。假设“Vertex”类中根本没有属性,我假设您可能不需要在图形中具有相同的精确顶点实例,而您可以在其他类中使用共享信息。另外,出于使用大多数图形算法的性能原因,您最有可能不将图形对象存储在关系数据库中。我将删除
hasMany
belongsTo
属性,并管理join类中的所有内容,以避免不必要的集合性能开销。有关更多信息,请参阅