Home > Net >  is there a way to do batch updates using HQL where the update values for each object are in a map?
is there a way to do batch updates using HQL where the update values for each object are in a map?

Time:09-09

I have a BibsService class which has a method assignBibNumbers that is responsible for assigning bib numbers to registrations.

This is the relevant code inside assignBibNumbers service method.

    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


        }



    }

The bibsMp has bib number and Registration object mapping. i.e the value part is registration object and it needs to be assigned the bib number which is the key.

so the bibsMp is of the following nature.

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

you can see when iterating through this map bibsMp

I assign the bib to that registration object.

Is there a faster way to achieve this using HQL. I was looking for a batch update method using HQL. I couldn't find the docs helpful.

I am using Grails 2.2 and from the docs https://grails.github.io/grails2-doc/2.2.0/guide/single.html

grails 2 uses Hibernate 3.6 and here is the docs of hibernate 3.6

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

i appreciate any guidance. Thanks!

CodePudding user response:

Hibernate will use JDBC batching if configured automatically, as long as you flush only at the end of your changes. Also see the Hibernate documentation for details about this: https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#batch

  • Related