Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

like image 381
Gordon Avatar asked Dec 27 '10 18:12

Gordon


People also ask

What is cascading in JPA?

In JPA, if any operation is applied on an entity then it will perform on that particular entity only. These operations will not be applicable to the other entities that are related to it. To establish a dependency between related entities, JPA provides javax.

What is default cascade type in hibernate?

CascadeType defaults to the empty array . See CascadeType in Annotation Type OneToOne. By default no operations are cascaded.

What is Cascade in hibernate?

Tags:cascade | hibernate. Cascade is a convenient feature to save the lines of code needed to manage the state of the other side manually. The “Cascade” keyword is often appear on the collection mapping to manage the state of the collection automatically.


1 Answers

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.

like image 191
axtavt Avatar answered Sep 30 '22 20:09

axtavt