CascadeType.REFRESH in Hibernate

By Arvind Rai, May 27, 2013
In Hibernate, CascadeType.REFRESH plays the role where more than one entity are associated to each other. CascadeType.REFRESH cascades the refresh operation to all associated entities. If one entity is refreshed, other associated entities will also be refreshed if CascadeType.REFRESH is specified.
For example if the entity Country is associated with State and we are refreshing entity, both the entities Country and State will be refreshed with current session of Hibernate. We need to call refresh() method to refresh 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.REFRESH)
	@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
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us