Hibernate and JPA cascading types when to use both

I use Hibernate for a project, and I'm confused about when to use org.hibernate.annotations.CascadeType and when to use javax.persistence.CascadeType annotations.

For example, when I should use something like this:

@OneToMany(fetch = FetchType.LAZY, cascade = javax.persistence.CascadeType.ALL) 

vs something like this:

 @OneToMany(fetch = FetchType.LAZY) @Cascade(org.hibernate.annotations.CascadeType.ALL) 

I also read that sleep mode will ignore some types of cascades that are in the annotations of xxxx. Maybe someone just planted me right?

+8
java hibernate jpa
source share
1 answer

In general, you will find many examples of such overlaps when using Hibernate. There may be several different reasons for this. Either because of obsolete reasons (Hibernate created the annotation before it got standardized in JPA), either because Hibernate supports more functionality than the JPA standard allows, or because they are slightly different in semantics.

In this case, the Hibernate documentation is very clear why @Cascade exists when @OneToMany(cascade=...) is the standard. Hibernate @Cascade ( @Cascade ) gives you more options than the standard JPA, and also has slightly different semantics.

You should always use standard JPA annotations unless you need the special Hibernate function / semantics.

+4
source share

All Articles