JPA和Hibernate级联混淆 [英] Confusion between JPA and Hibernate cascading

查看:89
本文介绍了JPA和Hibernate级联混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate 3.6并注释了我的代码(与使用hibernate映射文件相比)。我遇到了使用与Hibernate的CascadeType不兼容的JPA级联选项的已知问题(请参阅​​此链接了解更多信息 http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/ )。



我希望对问题做更多的澄清。我有一些特别的问题:
$ b 1)所以@Cascade({CascadeType.SAVE_UPDATE})适用于saveOrUpdate(),但它也适用于如果我使用merge()或坚持()?或者我必须使用全部三种Hibernate CascadeTypes?



2)如何决定是使用JPA级联选项还是使用Hibernate @Cascade注释?


$ b $ 2)在Hibernate中存在一个bug,但开发人员显然将此视为文档问题(我完全不同意他们),而我没有看到它在上述文件中提到。任何人都知道为什么这是按设计工作而不是Hibernate的JPA实现中的错误?



非常感谢。

解决方案

此行为记录在 11.11。传递持久性


  1. Hibernate级联类型对应于单独的操作,因此您需要全部三种。 / b>


  2. 在大多数情况下,您需要 CascadeType.ALL 或根本不需要级联。在这种情况下,JPA注释就足够了,因为JPA的 CascadeType.ALL 也覆盖了所有的Hibernate操作。否则,如果您需要细粒度的级联控制(并使用Hibernate的 Session 接口),则需要Hibernate的 @Cascade 这不是JPA实现中的错误,因为如果您使用JPA的 EntityManager 一切正常。只有将JPA批注与Hibernate的 Session 界面结合使用时,才会出现此问题。



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.

解决方案

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.

这篇关于JPA和Hibernate级联混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆