grailsgrails-ormgrails-3.3

Grails 3 Entity not saved when properties set in EntityClass


I occure a problem which I do not understand. Following code does not work:

AccountingEntity accountingEntity = AccountingEntity.get(params.id);

accountingEntity.setLifecycleStatusToArchived();

accountingEntity.save(flush:true);

Where the method setLivecylceStatusToArchived looks like:

void setLifecycleStatusToArchived() {
    this.lifecycleStatus = AccountingEntity.LIFECYCLE_ARCHIVED; //predefined static variable
    this.considerForRankingJob = false;
    this.dateArchived = new Date();
}

Problem is, that the entity is not updated. No validation erros when I use accountingEntity.validate() in advance.

However, this code works:

AccountingEntity accountingEntity = AccountingEntity.get(params.id);

accountingEntity.setDateArchived(new Date());
accountingEntity.setConsiderForRankingJob(false);
accountingEntity.setLifecycleStatus(AccountingEntity.LIFECYCLE_ARCHIVED);

accountingEntity.save(flush:true);

The code did not work any more after update from Grails 3.2.9 to 3.3.0.RC1 (Gorm 6.1.5) unless I followed all the steps in the guide (http://docs.grails.org/3.3.x/guide/upgrading.html) and the rest of the code is working properly (also database accesses etc.)

Has anybody an idea? What the problem could be?

Thanks in advance and best regards!


Solution

  • The short answer is dirty checking. When you are setting properties inside the instance method Grails doesn't know they are dirty.

    See the following github issue for how to resolve the problem:

    https://github.com/grails/grails-data-mapping/issues/961

    you have 2 options:

    call markDirty every time you change an internal field. This will be better for performance or as per http://gorm.grails.org/latest/hibernate/manual/index.html#upgradeNotes use

    hibernateDirtyChecking: true