gpt4 book ai didi

hibernate - 调用 findWhere 时 Grails 抛出 NonUniqueObjectException

转载 作者:行者123 更新时间:2023-12-02 15:35:41 24 4
gpt4 key购买 nike

我正在使用分配了 id 生成器的域类构建一个 grails 应用程序(也就是说,它们不会自动生成)。假设我有一个域类 Parent 与 static hasMany = [children: Child]和域类 Child。

我正在尝试使用 Parent.findWhere(propertyMap) 定位 Parent 的实例,但我得到一个 非唯一对象异常 表示已经有一个与此 session 关联的子实例,其 id 为 0。我的 propertyMap 不包含 id 的条目。

现在,每当我尝试在 session 中保存一个类的两个实例而不设置它们的 id 时,我已经习惯了得到这个错误。因为 id 不是自动生成的,所以它们默认为零,这允许保存第一个,第二个抛出错误,因为 id 是第一个的副本。令我惊讶的是,我试图找到一个实例,而不是保存一个......有谁知道可能导致这种行为的原因以及我如何在仍然使用我的属性图进行搜索的同时绕过它?

最佳答案

事实证明,这是一个时髦的语法糖问题。我在打电话 findWhere like

Parent.findWhere([ firstProperty: "firstValue", secondProperty: "SecondValue" ])

但显然我应该使用
Parent.findWhere( firstProperty: "firstValue", secondProperty: "SecondValue" )

(注意没有方括号)。我不知道为什么这会有所作为,但 findWhere 现在按预期工作。任何可能知道它为什么会这样表现的人都会加分。

关于hibernate - 调用 findWhere 时 Grails 抛出 NonUniqueObjectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11302615/

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