Grails : load proxy domain objects

26 / Jul / 2010 by Amit Jain 5 comments

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~~

FOUND THIS USEFUL? SHARE IT

Tag -

Grails load proxy

comments (5)

  1. Chris Sterling

    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.

    Reply
    1. amit

      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.

      Reply

Leave a Reply to Chris Sterling Cancel reply

Your email address will not be published. Required fields are marked *