Tree with JPA[/Hibernate]
Grrrrrr, trying to implement a very simple tree. It doesn't need to be bidirectional (since traversals are only top-down), so I assume it would be better (i.e. more space efficient) if it were unidirectional, although my mapping is failing:
@Entity
public class Node extends Model {
@Column(nullable=false, unique=true)
public String description;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public List<Node> children = new LinkedList<Node>();
public Node() {}
public Node(String description) {
this.description = description;
}
public void addChild(String description) {
Node child = new Node(description);
child.save();
this.children.add(child);
}
}
By "failing", I mean the child relation doesn't get mapped, the following unit test fails; the last assert
expects 1 but gets 0. In the CRUD section of the app I can see that both nodes are actually created and persisted, but the parent node doesn't have any children. From this I conclude that the mapping is broken:
public void testMenu() {
Node root = Node.find("byDescription", "root").first();
if (root == null) {
root = new Node("root");
root.save();
}
assertNotNull(root);
root.addChild("child 1");
JPA.em().flush();
JPA.em().getTransaction().commit();
JPA.em().getTransaction().begin();
JPA.em().clear(); // clear cache to grab the root from the db
root = Node.find("byDescription", "root").first();
assertEquals(1, root.children.size());
}
I'm using Play!, which is why all those members are public and not private, why there are no getters/setters, and why there isn't an Id field (the Id is inherited from the Model class). It is also why there are some non-standard looking functions there, like the find()
and save()
. They function very intuitively, and the access to public members is "converted" via reflection to invoke setters and getters that are provided or created.
This doesn't matter, it is my mapping that is broken so please try and focus on that rather than the side issues, which I'm quite sure aren't causing the problem.
Cheers.
Update:
The hibernate log:
Hibernate: /* from models.Node where description = ? */ select node0_.id as id35_, node0_.description as descript2_35_ from Node node0_ where node0_.description=? limit ?
Hibernate: /* insert models.Node */ insert into Node (description) values (?)
Hibernate: /* insert models.Node */ insert into Node (description) values (?)
Hibernate: /* from models.Node where description = ? */ select node0_.id as id35_, node0_.description as descript2_35_ from Node node0_ where node0_.description=? limit ?
Hibernate: /* load collection models.Node.children */ select children0_.Node_id as Node1_35_1_, children0_.children_id as children2_1_, node1_.id as id35_0_, node1_.description as descript2_35_0_ from Node_Node children0_ inne开发者_如何转开发r join Node node1_ on children0_.children_id=node1_.id where children0_.Node_id=?
It would help if you pasted the hibernate log.
It shouldn't be strictly necessary but you could try root.save() after root.addChild("child 1");
In addition you didn't commit the transaction, that wouldn't be a problem if the Node.find() uses the same session, but does it?
精彩评论