Hibernate:OnDelete 注释问题

发布于 2024-11-08 16:40:27 字数 1092 浏览 0 评论 0原文

你好 我有以下两个实体

@Entity
public class DocumentCollection {
  @Id
  @GeneratedValue
  private Long id;
  @OneToMany(targetEntity=Document.class,mappedBy="documentCollection",cascade=javax.persistence.CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  private Set<Document> documents;
 ...
}

并且:

@Entity
public class Document {
  @Id
  @GeneratedValue
  private Long id;

  @ManyToOne
  private DocumentCollection documentCollection;
  ...
}

当删除 DocumentCollection 时,所有引用的文档也应该被删除。 但我收到此错误:

Cannot delete or update a parent row: a foreign key constraint fails (`tms_db`.`document`, CONSTRAINT `FK3737353BEB85533C` FOREIGN KEY (`documentCollection_id`) REFERENCES `documentcollection` (`id`))

我还尝试将 @OnDelete 注释放入文档类中,但没有成功。 那么我错过了什么?供您参考,我使用的是 Hibernate 3.6.0.Final。

更新:我做了一个 mysql 模式转储,注意到文档表模式中没有 ON DELETE CASCADE 语句。 Hibernate 只生成外键约束,这会导致上述错误。有谁知道为什么休眠不生成“ON DELETE CASCADE”语句?

希望有人可以帮助我 谢谢

Hello
I have the following two entities

@Entity
public class DocumentCollection {
  @Id
  @GeneratedValue
  private Long id;
  @OneToMany(targetEntity=Document.class,mappedBy="documentCollection",cascade=javax.persistence.CascadeType.ALL)
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  private Set<Document> documents;
 ...
}

And:

@Entity
public class Document {
  @Id
  @GeneratedValue
  private Long id;

  @ManyToOne
  private DocumentCollection documentCollection;
  ...
}

When a DocumentCollection gets deleted, all referenced documents should be deleted too.
But I'm getting this error:

Cannot delete or update a parent row: a foreign key constraint fails (`tms_db`.`document`, CONSTRAINT `FK3737353BEB85533C` FOREIGN KEY (`documentCollection_id`) REFERENCES `documentcollection` (`id`))

I also tried putting the @OnDelete Annotation in the document class but didn't work.
So what am I missing? For your information, I'am using Hibernate 3.6.0.Final.

UPDATE: I did a mysql schema dump and noticed that there is no ON DELETE CASCADE statement in the document table schema. Hibernate only generates the foreign key constraints, which leads to the mentioned error. Has anybody an idea why hibernate does NOT generate the "ON DELETE CASCADE" statement?

Hope, somebody can help me
thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绅刃 2024-11-15 16:40:27

@OnDelete 注解影响 Hibernate 生成数据库模式的方式。如果您的架构不是由 Hibernate 生成的,或者添加此注释后尚未更新,则它将无法正常工作。

如果您想通过 @OnDelete(action = OnDeleteAction.CASCADE) 使用手动创建的数据库架构,则应在架构中手动将外键约束定义为删除级联

@OnDelete annotation affects the way Hibernate generates database schema. If your schema is not generated by Hibernate, or haven't been updated after adding this annotation, it wouldn't work correctly.

If you want to use manually created database schema with @OnDelete(action = OnDeleteAction.CASCADE), you should manually define foreign key constraint in your schema as on delete cascade.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文