【问题标题】:is there a way to do batch updates using HQL where the update values for each object are in a map?有没有办法使用 HQL 进行批量更新,其中每个对象的更新值都在地图中?
【发布时间】:2022-09-07 16:55:55
【问题描述】:

我有一个 BibsService 类,它有一个方法 assignBibNumbers 负责将围兜号码分配给注册。

这是 assignBibNumbers 服务方法中的相关代码。

    def bibsMp = getRegsBibsMap(compositeEvent, "asc", "date")


    def assignedBibNumber = false

    def totalAssignments = 0


    def failedParticipants = []

    bibsMp.each {

        def reg = it.value

        try{
            reg.participant.bibNumber = it.key
            reg.save()

            assignedBibNumber = true
            totalAssignments += 1

        }
        catch (Exception e){

            failedParticipants.add(reg)

            log.error reg.id + "  " + e


        }



    }

bibsMp 有 bib 号和 Registration 对象映射。即 value 部分是注册对象,需要为其分配作为 key 的 bib 号码。

所以 bibsMp 具有以下性质。

1 -> Registration#1 
2 -> Registration#2
3 -> Registration#3
4 -> Registration#4

您可以在遍历此地图 bibsMp 时看到

我将围兜分配给该注册对象。

有没有更快的方法来使用 HQL 来实现这一点。我正在寻找使用 HQL 的批量更新方法。我找不到有用的文档。

我正在使用 Grails 2.2 和来自文档 https://grails.github.io/grails2-doc/2.2.0/guide/single.html

grails 2 使用 Hibernate 3.6,这里是 hibernate 3.6 的文档

https://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/

我感谢任何指导。谢谢!

【问题讨论】:

    标签: hibernate grails hql


    【解决方案1】:

    如果自动配置,Hibernate 将使用 JDBC 批处理,只要您仅在更改结束时刷新。另请参阅 Hibernate 文档以了解有关此内容的详细信息:https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#batch

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 2019-08-28
    • 2023-04-11
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多