my thoughts on programming and other nerdy stuff

Play Framework 2.0, Ebean Lazy Loading and Object References that are null


I began having sporadic problems in my Play Framework 2.0.4 application where the attributes of referenced objects were null.

When getting a Contact object with ebean:
Contact contact = Contact.find.byId(id)

will be null.

However, this will return the correct value:

So I started googleing the problem and found this:

Enhancement of direct Ebean field access (enabling lazy loading) is only applied to Java classes, not to Scala. Thus, direct field access from Scala source files (including standard Play 2 templates) does not invoke lazy loading, often resulting in empty (unpopulated) entity fields. To ensure the fields get populated, either (angel) manually create getter/setters and call them instead, or (beer) ensure the entity is fully populated before accessing the fields.


This described the problem but offered a poor solution…

Then I found this thread with the answer: http://stackoverflow.com/questions/12977513/ewonetoone-relationship-with-play-framework-that-does-not-use-join

When loading the contact, you have to force the loading of the user as well!

Contact contact =
.where().eq("id", id)

If you have read this far, you may as well follow me on Twitter:

Author: poornerd

Tech­nol­o­gist, Entre­pre­neur, Vision­ary, Pro­gram­mer :: Grad­u­ated from USC (Uni­ver­sity of South­ern Cal­i­for­nia) with a degree in Com­puter Sci­ence. After 10+ years of free­lance con­sult­ing and pro­gram­ming, he co-founded Site­Force AG eBusi­ness Solu­tions in 1999 in Munich (München), Ger­many.


  1. Hi.

    Thanks for the time to write some help!. I had a similar problem, when i call a single Entity, with no relationship, only one table, the table’s class return null. I dont understand that Lazy Loading fail, and fully populated???, what tha heck is that?…. Thanks.

  2. I know that in the future, many people will pass over this post, i receantly discover that in play 2 you can’t running 2 @Test methods calling to the same Ebean Entity, because Ebean seems to lost context, so the entities will not populate themself in the second call to method @Test. I-m not using runnable so maybe or perhaps is the problem. I spend almost 2 days trying to figure out what tha hell is happening and i’m still with this problem.

Leave a Reply

Required fields are marked *.