lundi 20 avril 2015

Play Framework EBean fetch Object with Relations

I am trying to fetch a Question with all its Answers

Question

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

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

Answer

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

  @ManyToOne
  public Question question;
  ...
}

Query

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

fetch("answers") throws a Nullpointerexception.

Database tables question and answer are consistent. Table answer includes the correct foreign keys to the question table.

Stacktrace

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]

Aucun commentaire:

Enregistrer un commentaire