Play Framework EBean 获取具有关系的对象

Play Framework EBean fetch Object with Relations

我正在尝试获取 Question 及其所有 Answers

问题

@Entity
public class Question extends Model {
  @Id
  public Long id;

  @Constraints.Required
  @OneToMany(cascade = CascadeType.ALL)
  public ArrayList<Answer> answers = new ArrayList<>(4);
  ...
}

回答

@Entity
public class Answer extends Model{
  @Id
  public Long id;

  @ManyToOne
  public Question question;
  ...
}

查询

public static Result getQuestion() {
    return ok(
        Json.toJson(Question.find
        .fetch("answers")
        .orderBy("question.id")
        .setMaxRows(1)
        .findList()
        .get(0)
        )
    );
}

fetch("answers") 抛出 Nullpointerexception.

数据库table与questionanswer一致。 Table answer 包含 question table.

的正确外键

堆栈跟踪

play.api.Application$$anon: Execution exception[[NullPointerException: null]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$applyOrElse.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$$anonfun$applyOrElse.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
Caused by: java.lang.NullPointerException: null
at com.avaje.ebeaninternal.server.deploy.BeanDescriptor.getBeanDescriptor(BeanDescriptor.java:1411) ~[avaje-ebeanorm-3.3.4.jar:na]
at com.avaje.ebeaninternal.server.querydefn.OrmQueryDetail.setDefaultSelectClause(OrmQueryDetail.java:414) ~[avaje-ebeanorm-3.3.4.jar:na]
at com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery.setDefaultSelectClause(DefaultOrmQuery.java:842) ~[avaje-ebeanorm-3.3.4.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServer.createQueryRequest(DefaultServer.java:1105) ~[avaje-ebeanorm-3.3.4.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServer.createQueryRequest(DefaultServer.java:1093) ~[avaje-ebeanorm-3.3.4.jar:na]

可以查询不为null的"answer"

这应该有效

Question.find.fetch("answers").where().isNotNull("answers");

这可能有效

Question.find.fetch("answers").where().ne("answers.id", null);

问题是 Question class 中的这一行:

public ArrayList<Answer> answers = new ArrayList<>(4);

在 Play Framework 中,必须使用列表接口而不是实现:

public List<Answer> answers = new ArrayList<>(4);