I have a question regarding the JPA OR mapping between two persistent entities with a different CascadeType parameter on their annotations.
To clarify things, here is a simple example:
@Entity
public class Article
{
@Id
@GeneratedValue
private Long id;
@ManyToOne( cascade = CascadeType.ALL )
private Author author;
// Getters and Setters follow here
}
_
@Entity
public class Author
{
@Id
@GeneratedValue
private Long id;
@OneToMany( mappedBy = "author", cascade = CascadeType.REFRESH,
orphanRemoval = true )
private List< Article > articles;
// Getters and Setters follow here
}
As you can see, the “author” property has a different CascadeType set
(CascadeType.REFRESH) then the “articles” property (CascadeType.ALL). At first, I thought that a different CascadeType for the same property mapping is not allowed – but I tried it, and it is allowed.
Now, what I would like to know is, how does this behave? And makes such a (artifical) example any sense at all (as you see, this is more a theoretical question)?
Thanks a lot for your help!
cascade = CascadeType.XXXmeans: when you do the XXX operation on this object, automatically do the same XXX operation on the object(s) referenced by the association.So, in your case, if you persist/merge/delete an article, it will also persist/merge/delete its author. This is thus very questionable. I don’t think you really want that.
And when you’ll refresh an author, it will also refresh its articles.
Note that if you refresh an article, it will refresh its author (because of CascadeType.ALL), and since the association form author to articles also has the REFRESH cascade type, it will also refresh all the articles of the author.