Home  >  Hibernate

Example of CascadeType.REMOVE in Hibernate

By Arvind Rai, May 27, 2013
In Hibernate CascadeType.REMOVE plays the role when more than one entity is associated to each other. CascadeType.REMOVE cascades the remove operation of entity from hibernate session to all associated entities remove. If one entity is removed, other associated entities will also be removed in case CascadeType.REMOVE is annotated. For example if the entity Country is associated with State and we are removing entity states, both the entities Country and State will be removed with current session of hibernate. In Hibernate we need to call delete() method to delete the entity.

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.REMOVE)
	@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;
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us