{"id":1752,"date":"2010-09-16T18:41:19","date_gmt":"2010-09-16T13:11:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1752"},"modified":"2016-12-19T15:06:08","modified_gmt":"2016-12-19T09:36:08","slug":"gorm-dirty-checking-of-instance-and-properties","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/gorm-dirty-checking-of-instance-and-properties\/","title":{"rendered":"GORM dirty checking of instance and properties"},"content":{"rendered":"<p>Again I am going to showcase few useful methods that GORM provides us to check whether the object is dirty or not (again from <a href=\"http:\/\/www.grails.org\/doc\/latest\/\">grails.org<\/a>).<br \/>\nFirst question could be what is a dirty object?  An object is dirty if the current state of its properties does not match with its persistence values. It happens when you modify few properties of an object e.g.: <\/p>\n<pre lang=\"groovy\">\r\nPerson person = Person.get(1)\r\nprintln \"Output -: \" + person.firstname \/\/Output -: Uday  \r\nperson.firstname = \"Test\"\r\nprintln \"Output -: ${person.firstname}\" \/\/Output -: Test\r\nprintln \"Output -: ${person.isDirty()}\" \/\/Output -: True\r\nprintln \"Output -: ${person.isDirty('lastname')}\" \/\/Output -: False \r\nprintln \"Output -: ${person.isDirty('firstname')}\" \/\/Output -: True \r\n<\/pre>\n<p>There is one more good method to retrieve which properties of the object are dirty.<\/p>\n<pre lang=\"groovy\">\r\nprintln \"Output -: ${person.dirtyPropertyNames}\" \/\/Output -: ['firstname']\r\n<\/pre>\n<p>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 &#8220;not so good&#8221; 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:<\/p>\n<pre lang=\"groovy\">\r\nprintln \"Output -: ${ person.getPersistentValue('firstname')}\" \/\/Output -:Uday\r\n<\/pre>\n<p>A simple word in which I can define Grails is, it just &#8220;ROCKS&#8221;.<br \/>\n<\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Again I am going to showcase few useful methods that GORM provides us to check whether the object is dirty or not (again from grails.org). First question could be what is a dirty object? An object is dirty if the current state of its properties does not match with its persistence values. It happens when [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":21},"categories":[7],"tags":[411,412,413],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1752"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1752"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1752\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}