bi-directional in test_data.yml
If i have this code
@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="pare开发者_Python百科nt", insertable=false, updatable=false)
public Category parent;
public void addChild(Category child) {
Category root = this;
child.parent = root;
root.children.add(child);
root.save();
}
}
And i want to create test data in my test_data.yml file how I can type that there? I mean this is bi-directional..
For example if I would do it like this:
Category(root1):
title: root
children: [child1, child2]
Category(child1):
title: child1
parent: root1
Category(child2):
title: child2
parent: root1
I would get this error:
Cannot load fixture initial-data.yml: No previous reference found for object of type children with key child1
But if I would not type this: children: [child1, child2]
then I would have wrong structure, child1 and child2 would not ref to root.
I have done similar things on a similar model:
Category(cat5):
title: Cameras & Photo
Category(cat5.1): {title: Digital Camera, parent: cat5}
It works as long as I use the yaml data only for bootstrapping. Because, only the parent reference is set and the child list of the parent category remains uninitialized. In Hibernate this works because the foreign key is set correctly whatsoever. It's not pretty but it works for me. I have yet to figure out how to deal with those forward declarations in yaml.
精彩评论