2

我是 Ebeans / Play 2.0.4 的新手,我正在尝试对以下内容进行建模:

One question has many answers

所以我的课程看起来像这样:

问题模型:

package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;

import play.db.ebean.Model;

@Entity
public class Question extends Model {
    @OneToMany(cascade=CascadeType.ALL)
    public List<Answer> answers = new ArrayList<Answer>();
}

答案型号:

package models;

import javax.persistence.Entity;
import javax.persistence.Id;

import play.db.ebean.Model;

@Entity
public class Answer extends Model {
    @Id
    public Integer id;
} 

但是,这给了我以下错误:

java.lang.RuntimeException: No join columns

我究竟做错了什么?在生成的 DDL 中,我需要“更新级联”- CascadeType.ALL 是正确的类型吗?谢谢 :-)

4

1 回答 1

1

将 ID 添加到问题实体:

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

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

在后台,访问问题属性的 Ebean 会生成如下查询:

select * from question q join answer a where q.id = 123

在您的实体中缺少 q.id

于 2013-03-05T18:38:14.997 回答