在 Ebean 中创建嵌套对象的最简单方法是什么?

What is the simplest way to create nested objects in Ebean?

我需要两个名为 "States" 和 "Children" 的 Ebean 模型 classes。 "State" 对象可以包含嵌套的子对象(子列表)。

这里是基本状态class,

@Entity
public class States extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Constraints.Required(message = "stateName cannot be null")
    @Column(nullable = false)
    private String statename;

    @Column(nullable = true)
    private String url;

    @Column(nullable = true)
    private String parent;

    private List<Children> childrenList;
}

这里是基本的Children class,

@Entity
public class Children extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(nullable = false)
    private String statename;

    @Column
    private String child;
}

要使用 Ebean ORM 创建 State 对象,应该对这些 classes 做哪些最少的修改?我经历了 post、

Ebean Query by OneToMany Relationship

但是那里提出了很多更改建议。我只想要最少的修改。

我所要做的就是对 "States" class,

@Entity
public class States extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Constraints.Required(message = "stateName cannot be null")
    @Column(nullable = false)
    private String statename;

    @Column(nullable = true)
    private String url;

    @Column(nullable = true)
    private String parent;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Children> childrenList;
}

我在这里所做的唯一改变是,

@OneToMany(cascade = CascadeType.ALL)

我没有对"Children"class做任何改动。在启动播放应用程序之前,我设置了

play.evolutions.enabled = true

在 "application.conf" 文件中。然后使用在 "evolution.default" 文件夹中创建的 evolution SQL 文件,我调整了数据库的架构。之后 "States" 个对象与嵌套的 "Children" 个对象成功创建。