Home  >  Hibernate

Example of @OnDelete in Hibernate

By Arvind Rai, May 28, 2013
@OnDelete in hibernate is used when there are joined sub class. @OnDelete decides whether deleting an entry from database will delete the rows represented by joined sub class or not. There can be two action as below.
 @OnDelete(action=OnDeleteAction.CASCADE) 
 
We use OnDeleteAction.CASCADE when we need cascade delete.
 @OnDelete(action=OnDeleteAction.NO_ACTION)
 
We use OnDeleteAction.NO_ACTION when we need not to cascade delete. Find the example , how to use the @OnDelete.

@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")
	@OnDelete(action=OnDeleteAction.NO_ACTION)
	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