gpt4 book ai didi

grails - GORM:继承字段的更改不是isDirty()吗?

转载 作者:行者123 更新时间:2023-12-02 14:48:32 30 4
gpt4 key购买 nike

修改继承的多对一关系字段后,无法使用isDirty()或类似检查来检测到更改。
如何在不手动检查字段的情况下检测到更改?

我已经尝试过使用@DirtyCheck,但是这仅适用于简单的字段,例如lookupId@DirtyCheck不能修复关系字段,例如lookup

利用:

  • GORM,没有grails,特别是gorm-hibernate5-spring-boot:6.1.9.RELEASE。
  • Groovy 2.4.15
  • Java 8u151

  • 以下测试通过并显示当前不希望有的行为。
    @Unroll
    def 'MyObject dirty check with setter: #setter'() {
    given: '2 existing Lookups and a newly created MyObject'
    Lookup lookup1 = new Lookup(
    code: 'CODE1'
    ).save(flush: true, failOnError: true)
    Lookup lookup2 = new Lookup(
    code: 'CODE2'
    ).save(flush: true, failOnError: true)
    MyObject myObject = new MyObject(
    name: 'someName',
    lookup: Lookup.get('CODE1'),
    ).save(flush: true, failOnError: true)
    when: 'change the lookup'
    if(setter) {
    myObject.setLookup(Lookup.get('CODE2'))
    } else {
    myObject.lookup = Lookup.get('CODE2')
    }
    then: 'we can manually detect the change'
    myObject.lookup.code == 'CODE2'
    myObject.getPersistentValue('lookup').code == 'CODE1'
    myObject.getPersistentValue('lookup') != myObject.lookup
    myObject.getPersistentValue('lookup').code != myObject.lookup.code
    and: 'dirty checks do not detect the change'
    false == (myObject.lookup.getDirtyPropertyNames() ||
    myObject.getDirtyPropertyNames() ||
    myObject.lookup.isDirty() ||
    myObject.lookup.hasChanged() ||
    myObject.hasChanged() ||
    myObject.isDirty() ||
    ((LookupBase)myObject.lookup).isDirty() ||
    ((LookupBase)myObject.lookup).hasChanged() ||
    ((ModelBase)myObject).isDirty() ||
    ((ModelBase)myObject).hasChanged()
    )
    where:
    setter << [false, true]
    }

    有一个父类:
    @ToString(includeNames=true)
    @EqualsAndHashCode(
    includes = [ 'lookup', 'lookupId', ]
    )
    @AutoClone
    class ModelBase {
    UUID id

    Lookup lookup
    String lookupId
    }

    还有另一个父类:
    @ToString(includeNames=true)
    @EqualsAndHashCode(
    includes = [ 'code', ]
    )
    class LookupBase {
    String code

    static constraints = {
    code nullable: false
    }

    static mapping = {
    id name: 'code', generator: 'assigned'
    }
    }

    子类:
    @Entity
    @ToString(
    includeNames=true,
    includeSuperProperties=true,
    excludes = [ ... ]
    )
    @EqualsAndHashCode(
    callSuper = true,
    excludes = [ ... ]
    )
    class MyObject extends ModelBase implements GormEntity<MyObject> {
    String name
    }

    还有另一个 child 类:
    @Entity
    @ToString(includeNames=true, includeSuperProperties=true)
    @AutoClone
    class Lookup extends LookupBase implements GormEntity<Lookup> {
    static constraints = {
    code maxSize: 20
    }
    }

    最佳答案

    无需过多研究代码,您是否尝试过将@DirtyCheck添加到父类,子类或两个类定义中?当从非Domain(src/main/groovy)类继承的Domain类必须将显式检查显式添加到父类时,我看到了类似的行为。

    编辑:对不起,您看到您已经尝试过此操作的地方没有看到文章的结尾。

    关于grails - GORM:继承字段的更改不是isDirty()吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51369117/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com