I am using Hibernate on a project and I am throughly confused on when to use org.hibernate.annotations.CascadeType and when to use javax.persistence.CascadeType annotations.
For instance 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 have also read that hibernate will ignore certain cascade types that are in the one to xxxx annotations. Can someone set me straight?
In general you’ll find many examples of these kinds of overlapping when using Hibernate. There can be several different reasons for this. Either it’s because of legacy reasons (Hibernate created the annotation before it got standardized in JPA), or it’s because Hibernate supports more functionality than what the JPA standard allows, or it’s because they only differ slightly in their semantics.
In this case, the Hibernate documentation is very clear of why
@Cascadeexists when@OneToMany(cascade=...)is the standard. The Hibernate annotation (@Cascade) gives you many more options than the standard JPA one, and it also has slightly different semantics.You should always prefer using the standard JPA annotations as long as you don’t need some special Hibernate feature/semantics.