【问题标题】:bi-directional in test_data.ymltest_data.yml 中的双向
【发布时间】:2011-04-29 08:36:53
【问题描述】:

如果我有这个代码

@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();
    }
}

我想在我的 test_data.yml 文件中创建测试数据,如何在其中输入?我的意思是这是双向的..

例如,如果我会这样做:

 Category(root1):
   title: root
   children: [child1, child2]

 Category(child1):
   title: child1
   parent: root1

 Category(child2):
   title: child2
   parent: root1

我会收到这个错误:

无法加载夹具初始数据.yml: 未找到对象的先前参考 具有关键子项的子项1

但如果我不输入这个:children: [child1, child2],那么我的结构就会错误,child1child2 不会引用到 root。

【问题讨论】:

  • 我固定:类别(child1):标题:child1 类别(child2):标题:child2 类别(root1):标题:root 孩子:[child1,child2]

标签: hibernate jpa playframework


【解决方案1】:

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

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

只要我将 yaml 数据仅用于引导,它就可以工作。因为,只设置了父引用,而父类别的子列表保持未初始化。在 Hibernate 中,这是有效的,因为外键设置正确。它不漂亮,但对我有用。我还没有弄清楚如何处理 yaml 中的那些前向声明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2013-11-17
    相关资源
    最近更新 更多