我正在使用 Play 框架并尝试从我的 scala 代码访问 Ebean 实体的属性(定义为公共(public)字段并由 Play 框架增强)。
从 Play 文档中我了解到
(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.
那么我该怎么做 (b)?我可能可以在调用 Scala 之前从我的 Java 访问这些属性,但是有更好/更简单的方法吗?
最佳答案
假设您有一些具有 Address
属性的 Person
对象。
通常,在 EBean 中,您会执行如下操作:
Person p = Person.find.byId(1);
但是,p.address.street
可能会是 null
。
这就是为什么您可以定义明确的 getter 和 setter 并执行类似 p.getAddress().getStreet()
的原因。这是 (a) 部分。
但是还有另一种方法可以使它起作用。您在类中定义了一个方法来检索对象,但也获取引用的对象。
public static Person findById(Long id) {
return Ebean.find(Person.class)
.fetch("address")
.where()
.eq("id", id)
.findUnique();
}
我相信这是在开始与他们合作之前填充该字段的一种方式 (b)。
关于java - 如何完全填充 Ebean 实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329412/