Details
-
Type:
Sub-task
-
Status:
Closed
-
Priority:
Critical
-
Resolution: Fixed
-
Affects Version/s: 1.0.3
-
Fix Version/s: 1.1.1
-
Component/s: None
-
Labels:None
Description
An explanation of the current behaviour and why it's not exactly ideal is given here:
http://www.cacoethes.co.uk/blog/index.php?p=38
One possible solution is to use the Hibernate's Session.setReadOnly() method to ensure that changes to a domain instance are not persisted at flush time if there are any binding or validation errors.
Hopefully it would also be possible to call Session.setReadOnly(false) to make the changes persist if, for example, the save() succeeds after any binding errors have been manually rectified. It might be worth making the method available on domain instances directly too.
I added some thoughts from the mailinglist to this issue (as suggested from Peter).:
I'm wondering whether the "dirty check" feature of hibernate is a reasonable default for Grails at all.
Sure it's a cool feature: Whenever the session get flushed, hibernate checks all known objects if
some properties changed. Those objects get updated automatically to the DB.
But from my experience this feature often makes more trouble then benefits.
Many times people wondering why objects get saved 'magically'. They are used to
save objects explicitly.
Setting the FlushMode to MANUAL is a nice workaround in your example.
But from my understanding it prevents all objects from being flushed to DB?
What are you doing if another object, for example Author, needs to be updated in the same action?
Is there an easy way in Grails to deactivate hibernates dirty checking for all domain object by default?
(--> Changes on Objects (and cascaded children) get only stored if the save() method is called explicitly)