在EntityClass中设置属性时,Grails 3 Entity未保存

问题描述 投票:1回答:1

我发现了一个我不明白的问题。以下代码不起作用:

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

accountingEntity.setLifecycleStatusToArchived();

accountingEntity.save(flush:true);

方法setLivecylceStatusToArchived的位置如下:

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

问题是,实体没有更新。我事先使用会计Entity.validation()时没有验证错误。

但是,此代码有效:

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

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

accountingEntity.save(flush:true);

从Grails 3.2.9更新到3.3.0.RC1(Gorm 6.1.5)后代码不再起作用,除非我按照指南中的所有步骤(http://docs.grails.org/3.3.x/guide/upgrading.html)进行操作,其余代码工作正常(也是数据库)访问等)

有人有想法吗?问题可能是什么?

在此先感谢您的问候!

grails gorm grails-3.3
1个回答
2
投票

简短的回答是脏检查。当您在实例方法中设置属性时,Grails不知道它们是脏的。

有关如何解决此问题,请参阅以下github问题:

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

你有2个选择:

每次更改内部字段时调用markDirty。这将更好地表现或根据http://gorm.grails.org/latest/hibernate/manual/index.html#upgradeNotes使用

hibernateDirtyChecking: true
© www.soinside.com 2019 - 2024. All rights reserved.