load « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ load ’

Grails : load proxy domain objects

Monday, July 26th, 2010
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: , ,
Posted in Grails