Example of @OnDelete in Hibernate
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)
@OnDelete(action=OnDeleteAction.NO_ACTION)
@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; }