0

I'm working with this configuration in application.groovy:

grails.gorm.default.mapping = {
    id (generator: "identity")

    // send only the dirty fields to the database for updating
    dynamicUpdate (true)
    dynamicInsert (true)
}

The domain class:

package test

class Patient {
    Integer id
    Integer version

    String familyName
    String givenName

    Patient father
    Patient mother

    static constraints = {
        familyName (maxSize: 60)
        givenName (maxSize: 60)
        father ()
        mother ()
    }

    static mapping = {
        table 'patient'
        father (column: "father_id", sqlType: "int4")
        mother (column: "mother_id", sqlType: "int4")
    }

    def beforeUpdate () {
        println('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
        println('Record dirty? ' + this.isDirty())
        println('Dirty properties: ' + this.dirtyPropertyNames)
        println('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
    }
}

bootstrap.groovy:

package test

class BootStrap {

    def init = { servletContext ->
        Patient father = new Patient(familyName: 'Patient', givenName: 'Father').save()
        Patient mother = new Patient(familyName: 'Patient', givenName: 'Mother').save()
        Patient child = new Patient(familyName: 'Patient', givenName: 'Child', father: father, mother: mother).save()
    }
    def destroy = {
    }
}
  1. In browser click to see the Child
  2. Click Edit
  3. Don't change anything and click Update
  4. In the console you'll see, that noting is dirty but an update sql is being sent for version, father_id and mother_id - thought father_id and mother_id aren't being changed.

beforeUpdate XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Record dirty? false

Dirty properties: []

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2019-08-07 13:04:03.936 DEBUG --- [nio-8080-exec-4]

org.hibernate.SQL :

/* update
    test.Patient */ update
        patient 
    set
        version=?,
        father_id=?,
        mother_id=? 
    where
        id=? 
        and version=?

2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [1]

2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [1]

2019-08-07 13:04:03.938 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [INTEGER] - [2]

2019-08-07 13:04:03.939 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [INTEGER] - [3]

2019-08-07 13:04:03.939 TRACE --- [nio-8080-exec-4] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [INTEGER] - [0]

--

Why's that so? How to disable the unnecessarily update? The foreign keys will be updated with each of an update of another simple property, even if they are staying the same?

(hmm... I don't see the button to upload a small example project...)

Many thanks and a wonderful day to you :-)

rawi
  • 521
  • 3
  • 13
  • This maybe the same issue I logged here https://github.com/grails/gorm-hibernate5/issues/129 – virtualdogbert Aug 08 '19 at 01:19
  • Well no, I don't think those are the very same issues described: you have a problem with inheritance by unnecessarily full updating both objects upon a child's properties update. In my case I get always a dynamicUpdate of a changed simple object's property (String, Integer, etc) but always **supplementary** an update of the reference (foreign key) to another object, even if not changed; say the father_id 1234 will be updated unnecessarily to 1234 together with the simple (changed) property (**although** not among the dirtyPropertyNames). This could be of marginal interest for the most – rawi Aug 09 '19 at 09:31

0 Answers0