我正在尝试使用 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/