gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:57:05 24 4
gpt4 key购买 nike

我正在尝试获取一个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

数据库表questionanswer 是一致的。表 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]

最佳答案

可以查询不为空的“answer”

这应该可行

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

这可能有用

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

关于postgresql - Play Framework EBean 获取具有关系的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29672566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com