2
votes

I'm working with the Play framework and I'm trying to access properties (defined as public fields and enhanced by the Play framework) of an Ebean entity from my scala code.

From the play documentation I have learned that

(2) 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 templates) does not invoke lazy loading, often resulting in empty (unpopulated) entity fields. To ensure the fields get populated, either (a) manually create getter/setters and call them instead, or (b) ensure the entity is fully populated before accessing the fields.

So how would I go about doing (b)? I could probably access the properties from my Java before calling into scala but is there a better/simpler way?

1

1 Answers

5
votes

Let's say you have some Person object with an Address attribute.

Normally, in EBean, you would do something like:

Person p = Person.find.byId(1);

However, then it might happen, that p.address.street is null. That is why you can define explicite getters and setters and do something like p.getAddress().getStreet(). This is the (a) part.

But there is also another way to make this works. You define a method in your class to retrieve the object but also fetch the referenced ones.

public static Person findById(Long id) {
    return Ebean.find(Person.class)
        .fetch("address")
        .where() 
        .eq("id", id)
        .findUnique();
}

I believe this is a way to populate the field before starting to work with them (b).