简体   繁体   English

Hibernate self ManyToMany保存问题

[英]Hibernate self ManyToMany save problem

I have a class Node Node.java我有一个 class 节点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:我的要求:

  1. One node can reference many nodes.一个节点可以引用多个节点。
  2. One node can reference it self.一个节点可以自己引用它。
  3. One node can be referenced by many nodes or not.一个节点可以被多个节点引用,也可以不被多个节点引用。

I want to do something like this:我想做这样的事情:

  1. Node A -> Node B (Node A references Node B and so on)节点 A -> 节点 B(节点 A 引用节点 B 等等)
  2. Node A -> Node C节点 A -> 节点 C
  3. Node A -> Node D节点 A -> 节点 D

  4. Node B -> Node E节点 B -> 节点 E

  5. Node B -> Node C节点 B -> 节点 C

All steps run OK except the 5th one.除第 5 步外,所有步骤都运行正常。 I have an exception: Unique index or primary key violation: How can i tell Hibernate to DO NOT try to insert Node C but instead update it !我有一个例外: Unique index or primary key violation:我如何告诉 Hibernate 不要尝试插入节点 C 而是更新它!

You didn't provide the code which tries to create all entities.您没有提供尝试创建所有实体的代码。 However, the flow would be something like,然而,流程会是这样的,

  1. Create/Retrieve all nodes required for Node B创建/检索节点 B 所需的所有节点
  2. Populate Node B with all those用所有这些填充节点 B
  3. Save Node B with cascade-insert使用cascade-insert保存节点 B

The problem was not on Hibernate behalf but on a bug in my code:\问题不在 Hibernate 上,而是在我的代码中的一个错误:\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM