gpt4 book ai didi

grails - 为什么 Grails hasMany 列表大小错误?

转载 作者:行者123 更新时间:2023-12-02 15:05:59 26 4
gpt4 key购买 nike

我有一个域对象 Parent,它的 hasMany 关系实现为 List。我返回的列表大小不正确,我不知道为什么:

class Parent {
List children

static hasMany = [children : Child, otherHasMany : SomeOtherChildClass]

static mapping = {
children lazy: false
}
}

def parent = Parent.get(someId) // parent is returned as a result of a query
def numChildren = parent.children.size()

我得到很多空值散布在 child 列表中正确的 child 之间。

IE。
parent.children.each {
println it
}

给出:
Child 1
NULL
Child2
NULL
Child3
Child4
... // seemingly random order of NULLs interspersed between correct values, but there never appear to be 2 NULLs in a row

就我而言, size()调用返回 71 个子项,但父项中应该只有 51 个子项。

当我执行 SQL 查询时,我得到正确数量的 child :
SELECT count(*)  from CHILDREN where parent_id = someId
51

当我打开 SQL 日志并检查 Hibernate 正在执行的查询时,我得到了相同的正确答案 (51)。

请问我做错了什么?

最佳答案

像您创建的那样创建域时

class Parent {
List children
static hasMany = [children : Child]
}

然后为了完成这项工作,grails 添加了 children_idx Child 的列 table 。此列中的值很重要,因为 grails 使用它来制作列表(按顺序放置实例)。第一条记录的值将设置为 0 , 下一个 1 ,等等。和 如果这个序列被破坏,那么你在列表中得到 null .

这可能是问题所在。

为避免破坏顺序,请确保在删除关联对象时从其所有者中移除关联对象。

引用 Collections in GORM

关于grails - 为什么 Grails hasMany 列表大小错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29213809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com