Hibernate orphanRemoval删除我的父实体

I have a tree structure that I call a graph, that uses the adjacency list. Class Graph has

@OneToMany(mappedBy = "graphEntry", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@OptimisticLock(excluded = true)
public List<GraphNode> getStartNodes() {
    return startNodes;
}

public void addStartNode(GraphNode graphNode){
    startNodes.add(graphNode);
    graphNode.setGraphEntry(this);
}

public void removeStartNode(GraphNode graphNode){
    startNodes.remove(graphNode);
    graphNode.setGraphEntry(null);
}

GraphNode具有:

@ManyToOne
@JoinColumn(name = "graphEntry")
public Graph getGraphEntry() {
    return graphEntry;
}

@ManyToOne(fetch = FetchType.LAZY, targetEntity = GraphNode.class)
@JoinColumn(name = "parent")
@OptimisticLock(excluded = true)
public GraphNode getParentNode() {
    return parentNode;
}

@OneToMany(mappedBy = "parentNode", cascade = CascadeType.ALL, orphanRemoval = true) 
@OptimisticLock(excluded = true)
public List<GraphNode> getChildNodes() {
    return childNodes;
}

当我用其他节点替换startNode时,我得到

PersistentObjectException: detached entity passed to persist: ...GraphNode

当我更改2个节点的顺序时,我的数据已正确保存在DB中。

我试图将层叠类型更改为PERSIST和MERGE,而不是全部设置,但这无济于事。看起来当旧的startNode不再指向它时,hibernate正在删除Graph。

如何确保不删除我的图并且替换节点有效?

enter image description here

评论