saimiris_devel
saimiris_devel

Reputation: 667

Hibernate CascadeType inside association annotation vs outside

What is the difference between the CascadeType options inserted inside the @OneToMany annotation and those inserted inside @Cascade? which is the priority, is one of the two ignored?

    @OneToMany(mappedBy="page", fetch=FetchType.EAGER, cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE})
    private List<Tag> tags;

Upvotes: 0

Views: 1130

Answers (3)

Naou
Naou

Reputation: 717

As shown in hibernate documentation we can use both cascadeType attribute and @cascade annotation :

@OneToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@Cascade(org.hibernate.annotations.CascadeType.REPLICATE)
public Collection<Employer> getEmployers()

Upvotes: 1

Bilal BBB
Bilal BBB

Reputation: 1164

There is no difference.

If OneToMany is a JPA annotation (javax.persistence.OneToMany) you can not use Hibernate 'cascade' inside. You have to use JPA cascade.

@Cascade annotation is specific to hibernate and not JPA.

When you write something like : CascadeType.SAVE_UPDATE, that mean that the cascade is used when you use hibernate method 'saveOrUpdate', so when you use hibernate methods to save, delete, ... objects use @Cascade annotation. If you use JPA methods use cascade inside @OneToMany annotation.

Upvotes: 1

Naou
Naou

Reputation: 717

The Hibernate Documentation, is clear on this issues. They espically recommand to use both in case of SAVE_UPDATE.

Upvotes: 1

Related Questions