{"id":2473,"date":"2010-12-31T10:12:29","date_gmt":"2010-12-31T04:42:29","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2473"},"modified":"2016-12-16T12:45:40","modified_gmt":"2016-12-16T07:15:40","slug":"create-transients-using-formula-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/create-transients-using-formula-in-grails\/","title":{"rendered":"Create transients using formula in Grails"},"content":{"rendered":"<p>I would explain this through a use case of my own project.<\/p>\n<p>A Program has many AdvertisingMaterial and we have subclasses of AdvertisingMaterial, FlashAd, ImageAd etc. The user wants the ability to filter the programs which had flashAds, imageAds etc. This required filtering on the basis of the class property that we have in the\u00a0database table (When table tablePerHierarchy is true). So I did some changes in my domain class to get this property.<\/p>\n<p>[java]<br \/>\nclass AdvertisingMaterial {<br \/>\n        String className<\/p>\n<p>        static constraints = {<br \/>\n                className(nullable: true)<br \/>\n        }<\/p>\n<p>       static mapping = {<br \/>\n               className formula: &#8216;CLASS&#8217;<br \/>\n       }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now what I can use this className field in my dynamic finders and criteria query as well. So I can do something like:<\/p>\n<p>[java]<br \/>\nList&lt;AdvertisingMaterial&gt;adMaterials=AdvertisingMaterial.findAllByClassName(&quot;com.project.FlashAd&quot;)<br \/>\n[\/java]<\/p>\n<p>The only thing that we missed in this approach was, that we got the className property only with the persistent objects.<\/p>\n<p>If you do something like:<\/p>\n<p>FlashAd flashAd=new FlashAd()<\/p>\n<p>and call flashAd.className then you will get null in it because the object is not persisted into the database yet. If you want your transient even with the nonpersistent object then it might not be a good solution. Whereas you can do it the other way by creating a transient property and its getter method.<\/p>\n<p>This approach can be used in some other use-cases like:<\/p>\n<p>[java]<br \/>\nstatic mapping = {<br \/>\n               fullName formula: &quot;CONCAT(FIRST_NAME,&#8217;  &#8216;,LAST_NAME)&quot;<br \/>\n               totalAmount formula: &quot;SUM(AMOUNT)&quot;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Hope it helps!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would explain this through a use case of my own project. A Program has many AdvertisingMaterial and we have subclasses of AdvertisingMaterial, FlashAd, ImageAd etc. The user wants the ability to filter the programs which had flashAds, imageAds etc. This required filtering on the basis of the class property that we have in the\u00a0database [&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":15},"categories":[7],"tags":[480,482,483,481],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2473"}],"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=2473"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2473\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}