Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

I think I misunderstood the meaning of cascading in the context of a @ManyToOne relationship.

The case:

public class User {

   @OneToMany(fetch = FetchType.EAGER)

   protected Set<Address> userAddresses;

}

public class Address {

   @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

   protected User addressOwner;

}

What is the meaning of the cascade = CascadeType.ALL? For example, if I delete a certain address from the database, how does the fact that I added the cascade = CascadeType.ALL affect my data (the User, I guess)?

1 Answer

0 votes
by (119k points)

CascadeType.ALL means it will do all actions. 

CascadeType.PERSIST: While persisting an entity, it also persist the entities present in its fields. We recommend a liberal application of this cascade rule, because in case the EntityManager finds a field that references a new entity during the flush, and the field does not use CascadeType.PERSIST, then it is an error.

CascadeType.REMOVE: While deleting an entity, it also deletes the entities present in this field.

CascadeType.REFRESH: Whilerefreshing an entity, it also refreshes the entities present in this field.

CascadeType.MERGE: While merging entity state, it also merges the entities present in this field.

If you want to learn Java from the top experts, then I recommend this Java Certification program by Intellipaat.

Related questions

0 votes
1 answer
asked Nov 23, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Jul 22, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...