java - JPA Cascade注解和Hibernate Cascade注解的区别

标签 java hibernate hibernate-annotations hibernate-cascade

我正在尝试使用 Hibernate 注释。当我尝试使用级联时,我在 Eclipse intellisense 中得到两个选项:

    javax.persistence.CascadeType and org.hibernate.annotations.CascadeType

在 hibernate CascadeType 中,提供了比 JPA 中更多的选项。

有什么优势吗?

最佳答案

javax.persistence 注释是标准规范。 hibernate 注解代表了 Hibernate 的具体实现。它们大多重叠,但 Hibernate CascadeType 提供了一些特定于 Hibernate 的附加选项。

通常,尽可能使用 javax.persistence 注释。仅当您需要特定于 Hibernate 的选项之一并且知道您可能永远不需要切换到另一个持久性提供程序时,才使用 Hibernate 变体。

许多人在同一个项目中混合使用 JPA/Hibernate 注释,这是一种有效的做法。然而,可能会有像这样涉及 CascadeType 的微妙交互:

http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/

Look in the code, @OneToMany is from JPA , it expected a JPA cascade – javax.persistence.CascadeType. However when you save it with Hibernate session, org.hibernate.engine.Cascade will do the following checking

The Hibernate save process will causing a ACTION_SAVE_UPDATE action, but the JPA will pass a ACTION_PERSIST and ACTION_MERGE, it will not match and causing the cascade failed to execute.

关于java - JPA Cascade注解和Hibernate Cascade注解的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12546133/

相关文章:

hibernate - 如何防止出现 org.hibernate.loader.MultipleBagFetchException

java - 链表 NullPointerException

java - 在 Android 上的 ListFragment 中使用 ArrayAdapter 获取构造函数错误

java - SecondTable 可以是 OneToMany 关系吗?

java - 使用 Criteria 的 Hibernate 一对多搜索

java - 如何在hibernate注释中删除带有父键的子表记录?

java - 如何避免多个 if 语句

java - 带更新的最短路径算法

java - 使用 hibernate 注解映射 @ManyToMany 关系

java - 实体作为一对一关系上的键返回 "Composite-ID"错误