Hibernate self ManyToMany save problem
I have a class Node Node.java
public class WebPage implements BusinessObject {
@Id
private Long id;
@ManyToMany
@JoinTable(name = "childnodes")
private Set<WebPage> references; // nodes contained by this node
}
My requirements :
- One node can reference many nodes.
- One node can reference it self.
- One node can be referenced by many nodes or not.
I want to do something like this :
- Node A -> Node B (Node A references Node B and so on)
- Node A -> Node C
Node A -> Node D
Node B -> Node E
- Node B -> Node C
All steps run OK except the 5th one. I have an exception : Unique index or primary key violation:
How can i tell Hibernate to DO NOT try to insert Node C开发者_如何学C but instead update it !
You didn't provide the code which tries to create all entities. However, the flow would be something like,
- Create/Retrieve all nodes required for Node B
- Populate Node B with all those
- Save Node B with
cascade-insert
The problem was not on Hibernate behalf but on a bug in my code :\
精彩评论