Reputation: 1801
I have a problem, I have a domain class called Cicle that calculates the value of some of its properties by making an average from a collection of other domain class called Measurement, so if any of its Measurements is updated then the Cicle should be updated too.
The problem is that I calculate the average in the beforeUpdate method but this method isn't triggered if the Cicle attributes haven't changed, and as Cicle changes its attributes in the beforeUpdate method then the changes aren't being stored.
Is this the normal behavior? is there a way to force the save even if the attributes look the same? I tried flush: true, validate: false with no luck, and should I've been calling my method before saving all the time? or this is a bug and I should tell grails? or is there a best practice that I should implement instead?
Thanks
Upvotes: 1
Views: 1109
Reputation: 477
I have a domain class called Cicle that calculates the value of some of its properties by making an average from a collection of other domain class called Measurement, so if any of its Measurements is updated then the Cicle should be updated too.
Have you considered making the average a transient attribute? This would allow you to only have the computation done when it is needed, and save space in your database.
Upvotes: 2
Reputation: 24776
This is normal behavior. The reason why nothing is being saved is because nothing is being seen as "dirty" or changed. This is the default behavior for Hibernate, this is not a bug. "If nothing has changed, then don't bother saving anything."
As suggested in my comments you would likely be better off implementing an afterUpdate on your Measurements class to locate and update any effected Circles. This way you can encapsulate the call to recalculate the average there (which I assume will modify properties of the Circle and thus mark it as 'dirty' and in turn allow you to persist it).
Upvotes: 1