grails addTo 为 hasMan 关系输入重复数据

发布于 2024-11-08 16:37:25 字数 839 浏览 2 评论 0原文

我有一个非常简单的 hasMany 关系,其中“产品”有许多“包”。我正在运行一个脚本来尝试填充数据,第一次运行时效果很好。然而,在第二次运行时,它开始输入重复项,我认为这对于 hasMany 来说是不可能的。

package = Package.findBySourceId(packageId) ?: new Package(name:packageName, price:packagePrice, sourceId:packageId).save(flush:true)

product = Product.findBySourceId(productId)                 
product.addToPackages(package)
product.save(flush:true)

当我打开 sql 日志记录时,我可以看到有时应该在 addToPackages 调用上运行的选择没有运行。它只是执行选择来查找产品,然后直接插入到连接表中。

由于工作原因,我不想添加确切的查询,但基本上就像这样

Select -> for the initial package which it finds

Select -> for product which it finds by the product id

Insert -> insert into the join table without even a select to check if an entry exists

,但是有时它会运行选择并找到匹配项,而不是插入,并且该过程将重新开始。不知道这里 grails 背后的逻辑。请记住,我正在运行一个脚本,该脚本正在提取数千行数据,这是否是会话问题?

I have a pretty simple hasMany relationship where a "product" hasMany "packages". I'm running a script to try and fill in the data, which works fine on the first run. However on the second run, it starts inputing duplicates, which I though was not possible for a hasMany

package = Package.findBySourceId(packageId) ?: new Package(name:packageName, price:packagePrice, sourceId:packageId).save(flush:true)

product = Product.findBySourceId(productId)                 
product.addToPackages(package)
product.save(flush:true)

When I put on sql logging on, I can see that sometimes the select that should run on the addToPackages call is not run. It just does the select to find the product and then a straight insert into the join table.

I don't want to add the exact queries because of work but basically its like

Select -> for the initial package which it finds

Select -> for product which it finds by the product id

Insert -> insert into the join table without even a select to check if an entry exists

However sometimes instead of that insert it will run a select and find the match and the process will start over. No idea on the logic going on behind grails here. Keep in mind, I have a script running that's pulling the data with thousands of line, is this a session issue somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-11-15 16:37:25

您是否在一对多的“多”方面使用过 List 或 Set 来进行集合?默认值是一个 Set,它应该限制重复。如果您使用列表映射集合,那么您可能已经造成了问题。

或者,验证身份属性“package”对于 equals() 和 hashCode() 是否具有正确的行为。

Have you used a List or Set for your collection on the "many" side of your one-to-many? The default is a Set, which should constrain against duplicates. If you've mapped the collection using a List, then you may have created the problem.

Alternatively, verify that the identity property "package" has the correct behaviors for equals() and hashCode().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文