Home  >  Hibernate

Example of CascadeType.DETACH in Hibernate

By Arvind Rai, May 27, 2013
In Hibernate CascadeType.DETACH plays the role when more than one entity is associated to each other. CascadeType.DETACH cascades the detach operation to all associated entities detach from hibernate session. If one entity is detached, other associated entities will also be detached if CascadeType.DETACH is annotated. For example if the entity Country is associated with State and we are detaching entity, both the entities Country and State will be detached from current session of hibernate. In Hibernate we need to call detach() method to detach 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.DETACH)
	@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
comments powered by Disqus


Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us