Grails : load proxy domain objects
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~~
Excellent work, …thank you for sharing.! Carol Alrod
I really think this is a vital information for me. I should bookmark to your news feed. Thansk.
Another reason for us to upgrade to 1.3.x. Gonna have to bite the bullet soon.
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.
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.