I'm using Hibernate 3.6 and have my code annotated (versus using hibernate mapping files). I ran into the known "problem" of using JPA cascading options that are not compatible with Hibernate's CascadeType (see this link for more info http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/).
我正在使用Hibernate 3.6并对我的代码进行了注释(与使用hibernate映射文件相比)。我遇到了使用与Hibernate的CascadeType不兼容的JPA级联选项的已知“问题”(有关详细信息,请参阅此链接http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake /)。
I was hoping to get a bit more clarification on the problem. I have some particular questions:
我希望能对这个问题有一点澄清。我有一些特别的问题:
1) So @Cascade({CascadeType.SAVE_UPDATE}) works for saveOrUpdate(), but does it apply also if I use merge() or persist()? or do I have to use all three Hibernate CascadeTypes?
1)所以@Cascade({CascadeType.SAVE_UPDATE})适用于saveOrUpdate(),但如果我使用merge()或persist(),它是否也适用?或者我是否必须使用所有三种Hibernate CascadeTypes?
2) How do I decide whether to use JPA cascade options or the Hibernate @Cascade annotation instead?
2)如何决定是使用JPA级联选项还是使用Hibernate @Cascade注释?
2) There is a "bug" filed against this in Hibernate, but the developers apparently see this as a documentation issue, (I'm completely disagree with them), and I'm not seeing that it was addressed in said documentation. Anyone know why this is "working as designed" and not a bug in Hibernate's JPA implementation?
2)在Hibernate中存在针对此问题的“bug”,但开发人员显然将此视为文档问题(我完全不同意这些问题),并且我没有看到它在所述文档中得到解决。任何人都知道为什么这是“按设计工作”而不是Hibernate的JPA实现中的错误?
Many thanks in advance.
提前谢谢了。
2 个解决方案
#1
13
This behaviour is documented in 11.11. Transitive persistence.
此行为记录在11.11中。传递性持久性。
-
Hibernate cascade types correspond to the individual operations, so you need all three of them.
Hibernate级联类型对应于各个操作,因此您需要它们全部三个。
-
In most cases you need either
CascadeType.ALL
or no cascading at all. In that case JPA annotation is enough, since JPA'sCascadeType.ALL
covers all Hibernate operations as well. Otherwise, if you need fine-grained cascading control (and use Hibernate'sSession
interface), you need Hibernate's@Cascade
.在大多数情况下,您需要CascadeType.ALL或根本不需要级联。在那种情况下,JPA注释就足够了,因为JPA的CascadeType.ALL也涵盖了所有Hibernate操作。否则,如果你需要细粒度的级联控制(并使用Hibernate的Session接口),你需要Hibernate的@Cascade。
-
It's not a bug in JPA implementation, because if you use JPA's
EntityManager
everything works fine. This problem exists only if you combine JPA annotations with Hibernate'sSession
interface.这不是JPA实现中的错误,因为如果你使用JPA的EntityManager一切正常。只有将JPA注释与Hibernate的Session接口结合使用时,才会出现此问题。
#2
1
From Hibernate reference documentation
来自Hibernate参考文档
For each basic operation of the Hibernate session - including persist(), merge(), saveOrUpdate(), delete(), lock(), refresh(), evict(), replicate() - There is a corresponding cascade style
对于Hibernate会话的每个基本操作 - 包括persist(),merge(),saveOrUpdate(),delete(),lock(),refresh(),evict(),replicate() - 都有相应的级联风格
If you see CascadeType documentation, you will see each cascade style for each session operation
如果您看到CascadeType文档,您将看到每个会话操作的每个级联样式
How do I decide whether to use JPA cascade options or the Hibernate @Cascade annotation instead ?
我如何决定是使用JPA级联选项还是使用Hibernate @Cascade注释?
Prefer to use plain JPA cascade style when using a plain JPA application. If using Hibernate, prefer Hibernate cascade style
在使用普通的JPA应用程序时,更喜欢使用普通的JPA级联样式。如果使用Hibernate,则更喜欢Hibernate级联风格
#1
13
This behaviour is documented in 11.11. Transitive persistence.
此行为记录在11.11中。传递性持久性。
-
Hibernate cascade types correspond to the individual operations, so you need all three of them.
Hibernate级联类型对应于各个操作,因此您需要它们全部三个。
-
In most cases you need either
CascadeType.ALL
or no cascading at all. In that case JPA annotation is enough, since JPA'sCascadeType.ALL
covers all Hibernate operations as well. Otherwise, if you need fine-grained cascading control (and use Hibernate'sSession
interface), you need Hibernate's@Cascade
.在大多数情况下,您需要CascadeType.ALL或根本不需要级联。在那种情况下,JPA注释就足够了,因为JPA的CascadeType.ALL也涵盖了所有Hibernate操作。否则,如果你需要细粒度的级联控制(并使用Hibernate的Session接口),你需要Hibernate的@Cascade。
-
It's not a bug in JPA implementation, because if you use JPA's
EntityManager
everything works fine. This problem exists only if you combine JPA annotations with Hibernate'sSession
interface.这不是JPA实现中的错误,因为如果你使用JPA的EntityManager一切正常。只有将JPA注释与Hibernate的Session接口结合使用时,才会出现此问题。
#2
1
From Hibernate reference documentation
来自Hibernate参考文档
For each basic operation of the Hibernate session - including persist(), merge(), saveOrUpdate(), delete(), lock(), refresh(), evict(), replicate() - There is a corresponding cascade style
对于Hibernate会话的每个基本操作 - 包括persist(),merge(),saveOrUpdate(),delete(),lock(),refresh(),evict(),replicate() - 都有相应的级联风格
If you see CascadeType documentation, you will see each cascade style for each session operation
如果您看到CascadeType文档,您将看到每个会话操作的每个级联样式
How do I decide whether to use JPA cascade options or the Hibernate @Cascade annotation instead ?
我如何决定是使用JPA级联选项还是使用Hibernate @Cascade注释?
Prefer to use plain JPA cascade style when using a plain JPA application. If using Hibernate, prefer Hibernate cascade style
在使用普通的JPA应用程序时,更喜欢使用普通的JPA级联样式。如果使用Hibernate,则更喜欢Hibernate级联风格