CascadeType.ALL in Hibernate
May 27, 2013
In Hibernate, there are different cascade types as PERSIST, MERGE, REMOVE, REFRESH, DETACH. These cascadings are called by methods persist(), merge(), delete(), refresh(), detach(). If we want to cascade for all above situation, then we need to use CascadeType.ALL. If we use CascadeType.ALL, then for every action, all types of cascading will be achieved.
Find the example.
Country.java
@Entity public class Country implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="id") private int id; @Column(name="name") private String name; @OneToMany(cascade=CascadeType.ALL) @JoinColumn( name="id") private Set<State> states; }
State.java
@Entity @Table(name = "state") public class State implements Serializable{ private static final long serialVersionUID = 1L; @Id @Column(name = "id") private int id; @Id @Column(name = "country_id") private int countryId; @Column(name = "name") private String name; }