在游戏框架中保留对象列表

persisting a list of objects in play framework

我有一个名为 Device.class 的 class,我想要一个字段,它是 CommentObj.class 的列表。问题是这个字段没有保存在数据库中(我使用的是 Ebean)。怎么可能这样做?

@Entity
public class Device extends Model {

@Id
public Long id;

@Constraints.MaxLength(50)
@Constraints.Required
public String serialNo;

...

public List<CommentObj> comments = new ArrayList<CommentObj>();

public class CommentObj extends Model {

@Id
public Long id;


@Constraints.MaxLength(50)
public String author;

@Constraints.MaxLength(500)
@Constraints.Required
public String content;

@Constraints.Required
public Date date;

public static Finder<Long, CommentObj> find = new Finder<>(Long.class, CommentObj.class);

public CommentObj(String author, String content){
    this.author = author;
    this.content = content;
    date = new Date();
}

}

这里是数据库中Device的结构(根据1.sql)。没有评论区的迹象

create table device (
  id                        bigint not null,
  serial_no                 varchar(255),
  invent_no                 varchar(255),
  name                      varchar(255),
  device_type_id            bigint,
  description               varchar(2000),
  cal_date                  timestamp,
  cal_duration_in_months    integer,
  setup_id                  bigint,
  manufacturer_id           bigint,
  constraint pk_device primary key (id))
;

假设您的 comment_obj table 中确实有一个 device_id,您可以通过将以下注释添加到 Device.class' comments 字段:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName = "id", name = "device_id")
public List<CommentObj> comments = new ArrayList<CommentObj>();