Confusion between JPA and Hibernate cascading
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/).
I was hoping to get a bit more clarification on the problem. I have some particular questions开发者_C百科:
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?
2) How do I decide whether to use JPA cascade options or the Hibernate @Cascade annotation instead?
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?
Many thanks in advance.
This behaviour is documented in 11.11. Transitive persistence.
Hibernate cascade types correspond to the individual operations, so you need all three of them.
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
.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.
From Hibernate reference documentation
For each basic operation of the Hibernate session - including persist(), merge(), saveOrUpdate(), delete(), lock(), refresh(), evict(), replicate() - There is a corresponding cascade style
If you see CascadeType documentation, you will see each cascade style for each session operation
How do I decide whether to use JPA cascade options or the Hibernate @Cascade annotation instead ?
Prefer to use plain JPA cascade style when using a plain JPA application. If using Hibernate, prefer Hibernate cascade style
精彩评论