Search code examples
grailsgrails-orm

Grails behavior method AddTo*


I'm an instability in my addTo* method which I have a record that constantly have to make the relationship information in one instance . The problem is that when I place multiple inserts in a short time , they cease to be persisted in the database and not after a they return , they are simply discarded time.

My simple class is ' Occurrence ' and ' Monitoring ' , whenever I make a call I have to register it and it occurred on and after 2 consecutive records the 3rd no longer persists in the database and I lose the record .

Class Occurrence implements Serializable{
...
hasMany = [accompaniments: Monitoring]
...
}

Class Monitoring implements Serializable{
...
belongsTo = [occurrence : Occurrence]
...
}

Have the controller looks like this:

def regMonitoring(Long id){

        def chamadoInstance = Occurrence.get(id)

        if (!chamadoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'Occurrence .label', default: 'Occurrence '), id])
            return
        }

        chamadoInstance.status = StatusChamado.findByCodigo("MOV") 

        if (!chamadoInstance.save(flush: true)) {
            render(view: "editMonitoring", model: [chamadoInstance: chamadoInstance])
            return
        }


        def mov = chamadoService.regMonitoring("") //returns an instance of Customer with preset output values ​​.

        chamadoInstance.addToAccompaniments (mov) 

        redirect(action: "showChamado", id: chamadoInstance.id)


    }

Solution

  • Add in a save of your domain instance after you add to the collection.

    chamadoInstance.addToAccompaniments (mov) 
    chamadoInstance.save(flush: true)
    

    This should solve the issue.