Having a case in one of my domain class, we save the transition field to the Mongo database in hook beforeInsert and afterUpdate, which works fine with the following conditions: -
- The inserts work great without any problems.
- Updates work well if there is a modified intransient field
The isDirty task works for non-transient properties.
The code is as follows:
class ResoruceInstance { def configurationService Status status //Transient Map<String, Object> configuration static transients = ['configuration'] public Map<String, Object> getConfiguration() { if(!configuration) configuration = configurationService.get(id, CollectionConstants.RESOURCE_INSTANCE_IDENTIFIER) return configuration } def afterInsert() { configurationService.save(id, CollectionConstants.RESOURCE_INSTANCE_IDENTIFIER, configuration) } def afterUpdate() { if(this.isDirty("configuration")) configurationService.save(id, CollectionConstants.RESOURCE_INSTANCE_IDENTIFIER, configuration) } }
To deal with this problem, I created isDirtyMongo ('transient_field'). This works well until the non-transient property changes, since afterUpdate is called only for transient properties.
The modified hook is as follows:
def afterUpdate() { if(this.isDirtyMongo("configuration")) configurationService.save(id, CollectionConstants.RESOURCE_INSTANCE_IDENTIFIER, configuration) } boolean isDirtyMongo(String property){ //return whether is dirty or not }
So, the final question is how can we also call the update hook for the transition field changes.
Any help would be greatly appreciated.
source share