GORM provides Grails developers lots of useful hooks, events, methods, and functionality, very important is domain events that inform you when domain objects are being created, updated, and deleted. Within these events, a common use-case is to determine if a particular domain property has changed, and to act accordingly. Below is a code snippet of how you validate your domain before insert or update. |
package com.pkm class Table1 { Long id String name String roll static mapping = { table("table1") } static hasMany = [ scores: Table2 ] void beforeValidate(){ println("BEFORE VALIDATE") } void beforeInsert(){ println("BEFORE INSERT") } void beforeUpdate(){ println("BEFORE UPDATE") } } |
Now, how can you get the actual value of that object? The actual value is modified and you want to get the persisted value. One “not so good” idea is to call refresh() which will load the object from the database again which might be a huge data retrieval in few cases. To solve this problem, GORM has given a simple method which will do the work for you: Below is a example of how you can see which fields are updating in current transaction using below code snippet |
void beforeUpdate(){ println("BEFORE UPDATE") this.dirtyPropertyNames.each { String fieldName -> def oldValue = this.getPersistentValue(fieldName) def newValue = this.getProperty(fieldName) println("\t> VALUE CHANGED FOR $fieldName FROM $oldValue TO $newValue") } } |
The best place to check for dirty properties is usually in the beforeUpdate event. The beforeInsert and afterUpdate events may also seem like good choices at first, but they may not be depending on the use-case. Before inserting, all properties are dirty because they are new, so they haven't really changed they have just been created. After updating, none of the properties are dirty because the new values have already been persisted in your database. These events provide plenty of value if used correctly, but may not always be the best place to check for dirty domain properties. |
One domain in particular that I always perform dirty property checks on is any User domains that my application requires. For instance, if a User object's password has changed, you may want to be alerted in the beforeUpdate event and encrypt the password before it is inserted into your database. |
You also can check if an existing field is dirty or not using this.isDirty("fieldname") |
Be careful not to perform any saves on the domain within these events, as you will get a StackOverflowException. (The event calls .save(), restarting the whole event cycle, where .save() will be called again, and so on. This infinite loop will never end, and the exception will be raised.) |
Otherhand you can check if entire session (hibernate session) is dirty or not. so below is an example how to check if whole hibernate session is dirty or not. |
package com.pkm import grails.transaction.Transactional import org.springframework.transaction.TransactionStatus import org.hibernate.SessionFactory import grails.util.Holders @Transactional class HomeService { TransactionStatus transactionStatus void callMe() { Table1 table1 = Table1.first() table1.roll = System.currentTimeMillis().toString() table1.save() SessionFactory sessionFactory = Holders.applicationContext.getBean(SessionFactory) Boolean isDirty = sessionFactory.currentSession.isDirty() println("IS SESSION DIRTY=${isDirty.toString().capitalize()}") } } |
Showing posts with label dirty-checking. Show all posts
Showing posts with label dirty-checking. Show all posts
Friday, July 6, 2018
Grails on Groovy > GORM dirty checking of instance and properties > grails - tell me if anything is dirty > Checking for Dirty (Updated) Properties on a Grails Domain
Subscribe to:
Posts (Atom)