Reputation: 533
I am going through spring project and in some model classes there is type
cascade={CascadeType.ALL}
written in parameters for eg: ,
@ManyToOne(fetch = FetchType.EAGER,cascade=CascadeType.ALL) @JoinColumn(name="USER_ID", nullable=false)
private User user;
My question is in what purpose we should use this ?
Thanky you.
Upvotes: 3
Views: 1584
Reputation: 112
This attribute means that ALL (because CascadeType.ALL) operations associated with objects of the class (Outer class) will be executed for associated object of class User (Inner class).
For example:
@Entity
public class Group {
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name="USER_ID", nullable=false)
private User user`
If you will try to remove Group from DB it will cause removing of associated user.
Enum CascadeType will help you to specify which kind of operations you want to perform with associated user.
If you want to specify cascading execution just for removing and persist you have to do something like that:
@ManyToOne(cascade = {CascadeType.REMOVE, CascadeType.PERSIST}, fetch = FetchType.EAGER)
Upvotes: 5