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.
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.
CascadeType defaults to the empty array . See CascadeType in Annotation Type OneToOne. By default no operations are cascaded.
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.
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'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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With