grails / gorm-hibernate5

GORM for Hibernate 5
Apache License 2.0
65 stars 70 forks source link

Not allow to modify dateCreated ? #149

Open abcfy2 opened 5 years ago

abcfy2 commented 5 years ago

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 ?

codeconsole commented 5 years ago

I think this is related to this?

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

abcfy2 commented 5 years ago

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.

tircnf commented 10 months ago

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)
}```