Grails : load proxy domain objects « Intelligrape Groovy & Grails Blogs

Grails : load proxy domain objects

Posted by Amit Jain

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’t retrieve the record from the database until property other than id is accessed.
Let us consider a scenario, where we have id of a Object “subject” and students of that subject needs to be listed. So we would generally do something like as given below:

Subject subject = Subject.get(subjectId)
Student.findBySubject(subject)

In the above code, we had to load subject unnecessarily where its ‘id’ should have been sufficient. Now, with load no extra queries are required.

Subject subject = Subject.load(subjectId)  //creates a proxy object, not retrieved from database 
Student.findBySubject(subject)

And same when used with criteria queries

Student.list{
  eq('subject', Subject.load(subjectId))
  ...
}

Thanks to the grails development team for all their efforts!

Cheers!!

~~Amit Jain~~
amit@intelligrape.com

http://www.IntelliGrape.com

  • Share/Bookmark
Tags: , ,
This entry was posted on July 26th, 2010 at 6:14 pm and is filed under Grails . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “Grails : load proxy domain objects”

  1. Do you know if this works with Grails version 1.2.x? I got a compilation failure when trying to do this even though it seems that the method exists. Thanks for a great article.

  2. amit says:

    Thanks Chris. I couldn’t find load() in grails docs for grails version 1.2.x. I believe it was introduced in 1.3.x version.

  3. Another reason for us to upgrade to 1.3.x. Gonna have to bite the bullet soon.

  4. I really think this is a vital information for me. I should bookmark to your news feed. Thansk.

Leave a Reply