Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JPA Cascade annotations and Hibernate Cascade annotation

I was trying some hands on with Hibernate annotations. When I tried to use Cascading then I get two options in the Eclipse intellisense :

    javax.persistence.CascadeType and org.hibernate.annotations.CascadeType

In hibernate CascadeType, there are many options given than in JPA's one.

Is there any advantage of one over another?

like image 723
Anand Avatar asked Sep 22 '12 17:09

Anand


1 Answers

The javax.persistence annotations are a standards specification. The hibernate annotations represent Hibernate's specific implementation. They mostly overlap, but the Hibernate CascadeType provides some additional options specific to Hibernate.

Generally, use the javax.persistence annotations whenever possible. Use the Hibernate variant only if you need one of the options specific to Hibernate and know that you likely never need to switch to another persistence provider.

Many people mix the JPA/Hibernate annotations within the same project which is a valid thing to do. However, there can be subtle interactions like this one involving CascadeType:

http://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/

Look in the code, @OneToMany is from JPA , it expected a JPA cascade – javax.persistence.CascadeType. However when you save it with Hibernate session, org.hibernate.engine.Cascade will do the following checking

The Hibernate save process will causing a ACTION_SAVE_UPDATE action, but the JPA will pass a ACTION_PERSIST and ACTION_MERGE, it will not match and causing the cascade failed to execute.

like image 186
kaliatech Avatar answered Sep 30 '22 15:09

kaliatech