我正在尝试使用其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
。
数据库表question
和answer
是一致的。表answer
包含question
表的正确外键。
堆栈跟踪
play.api.Application$$anon$1: 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$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.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]
答案 0 :(得分:0)
您可以查询非空的“答案”
这应该有效
Question.find.fetch("answers").where().isNotNull("answers");
这可能有效
Question.find.fetch("answers").where().ne("answers.id", null);
答案 1 :(得分:0)
问题是Question
类中的这一行:
public ArrayList<Answer> answers = new ArrayList<>(4);
在Play Framework中,必须使用列表界面而不是实现:
public List<Answer> answers = new ArrayList<>(4);