{"id":1394,"date":"2010-07-26T18:14:30","date_gmt":"2010-07-26T12:44:30","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1394"},"modified":"2015-07-09T14:59:44","modified_gmt":"2015-07-09T09:29:44","slug":"grails-load-proxy-domain-objects","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-load-proxy-domain-objects\/","title":{"rendered":"Grails : load proxy domain objects"},"content":{"rendered":"<p>Hi Friends,<\/p>\n<p>I was going through grails docs, encountered a method called load(), found it really useful thought would share with you. What load() does is, it creates a proxy object and doesn&#8217;t retrieve the record from the database until property other than id is accessed.<br \/>\nLet us consider a scenario, where we have id of a Object &#8220;subject&#8221; and students of that subject needs to be listed. So we would generally do something like as given below:<\/p>\n<div class=\"code\">\n<pre lang=\"groovy\">Subject subject = Subject.get(subjectId)\r\nStudent.findBySubject(subject)\r\n<\/pre>\n<\/div>\n<p>In the above code, we had to load subject unnecessarily where its &#8216;id&#8217; should have been sufficient. Now, with load no extra queries are required.<\/p>\n<div class=\"code\">\n<pre lang=\"groovy\">Subject subject = Subject.load(subjectId)  \/\/creates a proxy object, not retrieved from database \r\nStudent.findBySubject(subject)\r\n<\/pre>\n<\/div>\n<p>And same when used with criteria queries<\/p>\n<div class=\"code\">\n<pre lang=\"groovy\">Student.list{\r\n  eq('subject', Subject.load(subjectId))\r\n  ...\r\n}\r\n<\/pre>\n<\/div>\n<p>Thanks to <a title=\"Best Grails Development Team\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">the grails development team<\/a> for all their efforts!<\/p>\n<p>Cheers!!<\/p>\n<p>~~Amit Jain~~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Friends, I was going through grails docs, encountered a method called load(), found it really useful thought would share with you. What load() does is, it creates a proxy object and doesn&#8217;t retrieve the record from the database until property other than id is accessed. Let us consider a scenario, where we have id [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[4840,347,346],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1394"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1394"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1394\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}