Example of CascadeType.REFRESH in Hibernate

By Arvind Rai, May 27, 2013
In Hibernate CascadeType.REFRESH plays the role when more than one entity is associated to each other. CascadeType.REFRESH cascades the refresh operation to all associated entities refresh by hibernate session. If one entity is refreshed, other associated entities will also be refreshed if CascadeType.REFRESH is annotated. 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. In 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