Hibernate 双向输入测试数据.yml

Hibernate 双向输入测试数据.yml,hibernate,jpa,playframework,Hibernate,Jpa,Playframework,如果我有这个密码 @Entity public class Category extends Model { public String title; public Category() {} public Category(String title) { this.title = title; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

如果我有这个密码

@Entity
public class Category extends Model {

    public String title;

    public Category() {}

    public Category(String title) {
        this.title = title;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name="parent")
    public List<Category> children = new LinkedList<Category>();

    @ManyToOne
    @JoinColumn(name="parent", insertable=false, updatable=false)
    public Category parent;

    public void addChild(Category child) {
        Category root = this;
        child.parent = root;
        root.children.add(child);
        root.save();
    }
}
我会得到这个错误:

无法加载fixture initial-data.yml: 未找到对象的以前引用 具有关键child1的类型childs


但如果我不键入以下内容:children:[child1,child2],那么我的结构就会错误,child1和child2不会引用root。

我在类似的模型上做过类似的事情:

Category(cat5):
  title: Cameras & Photo
Category(cat5.1): {title: Digital Camera, parent: cat5}

只要我将yaml数据仅用于引导,它就可以工作。因为,仅设置父引用,并且父类别的子列表保持未初始化状态。在Hibernate中,这是因为外键设置正确。虽然不漂亮,但对我来说很管用。我还没有弄清楚如何处理yaml中的这些转发声明。

我修复了:Categorychild1:title:child1 Categorychild2:title:child2 Categoryroot1:title:root child1,child2]
Category(cat5):
  title: Cameras & Photo
Category(cat5.1): {title: Digital Camera, parent: cat5}