3

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.

1
  • 1
    Googling "hibernate cascade" should provides you some answsers... Commented Sep 2, 2016 at 7:56

1 Answer 1

5

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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.