14

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.

Gordon
  • 1,210
  • 5
  • 16
  • 23

2 Answers2

13

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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 1. If I understand correctly in that Hibernate is implementing JPA, I would have expected Hibernate's SAVE_UPDATE to cascade to JPA's persist() and merge(), but I understand why I need all three. – Gordon Dec 28 '10 at 21:27
  • 2. This bothers the snot out of me - ALL will work for both, but if you don't use ALL, Hibernate isn't "smart" enough to do the mapping for you. I also see nothing in the Hibernate documentation about the use of @Cascade with Hibernate Sessions (and this is why I think the bug report is valid). – Gordon Dec 28 '10 at 21:30
  • 2
    @Jay: The point is that Hibernate acts as a JPA implementation when accessed via `EntityManager`, if Hibernate is accessed via `Session` it acts on its own, though it still can use JPA annotations. – axtavt Dec 29 '10 at 10:53
1

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

Arthur Ronald
  • 33,349
  • 20
  • 110
  • 136
  • Just FYI, CascadeType.SAVE_UPDATE isn't mentioned at all in the manual, "delete" applies to XML for the delete() method, but CascadeType.REMOVE applies to annotations for the delete() method. and CascadeType.REFRESH, CascadeType.EVICT and CascadeType.REPLICATE aren't mentioned in the manual either. – Gordon Dec 28 '10 at 21:47