Confusion between JPA and Hibernate cascading

不打扰是莪最后的温柔 提交于 2019-11-30 06:58:38

问题


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:

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.


回答1:


This behaviour is documented in 11.11. Transitive persistence.

  1. Hibernate cascade types correspond to the individual operations, so you need all three of them.

  2. In most cases you need either CascadeType.ALL or no cascading at all. In that case JPA annotation is enough, since JPA's CascadeType.ALL covers all Hibernate operations as well. Otherwise, if you need fine-grained cascading control (and use Hibernate's Session interface), you need Hibernate's @Cascade.

  3. 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's Session interface.




回答2:


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



来源:https://stackoverflow.com/questions/4540806/confusion-between-jpa-and-hibernate-cascading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!