gorm-hibernate5 icon indicating copy to clipboard operation
gorm-hibernate5 copied to clipboard

Not allow to modify dateCreated ?

Open abcfy2 opened this issue 6 years ago • 3 comments

Issue link: grails/grails-data-mapping#1249

When GORM version is greater than 6.1.11, will nolonger to change the dateCreated anymore.

I find the reason is:

https://github.com/grails/gorm-hibernate5/blob/408b681658ee24ff13db3b6c20c9ea290756ec3b/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/support/ClosureEventTriggeringInterceptor.java#L205-L208

I don't know why you're not allow to modify dateCreated ?

abcfy2 avatar Aug 21 '19 16:08 abcfy2

I think this is related to this?

https://github.com/grails/gorm-hibernate5/issues/146

codeconsole avatar Aug 25 '19 03:08 codeconsole

Yes, that's true. Since GORM 6.0.11 does not allow to modify dateCreated, and I find the source code. If you changed the dateCreated, it will be dismissed.

abcfy2 avatar Aug 25 '19 05:08 abcfy2

https://gorm.grails.org/latest/hibernate/manual/index.html section 8.1.9

Make sure and flush the session is flushed inside the closure.

//Only the dateCreated property handling will be disabled for only the Foo domain
autoTimestampEventListener.withoutDateCreated(Foo) {
    new Foo(dateCreated: new Date() - 1).save(flush: true)
}

//Only the lastUpdated property handling will be disabled for only the Foo and Bar domains
autoTimestampEventListener.withoutLastUpdated(Foo, Bar) {
    new Foo(lastUpdated: new Date() - 1, bar: new Bar(lastUpdated: new Date() + 1)).save(flush: true)
}

//All timestamp property handling will be disabled for all domains
autoTimestampEventListener.withoutTimestamps {
    new Foo(dateCreated: new Date() - 2, lastUpdated: new Date() - 1).save(flush: true)
    new Bar(dateCreated: new Date() - 2, lastUpdated: new Date() - 1).save(flush: true)
    new FooBar(dateCreated: new Date() - 2, lastUpdated: new Date() - 1).save(flush: true)
}```

tircnf avatar Dec 19 '23 17:12 tircnf