Here is a cascading deletion of the demo, where garage and auto is a one-to-many relationship.
The key to cascading deletions is to add a cascadetype.remove callout to the parent bar.
Garage.java
/**
* Many to one pair of multiple associations
*/
Java code
Package com.jpa.bean1;
Import Java.util.HashSet;
Import Java.util.Set;
Import Javax.persistence.CascadeType;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.FetchType;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import Javax.persistence.OneToMany;
@Entity
public class Garage {
Private Integer GID;
Private String Garagenum;
Private set<auto> Autos = new hashset<auto> ();
@Id @GeneratedValue
Public Integer Getgid () {
return GID;
}
public void SetGid (Integer gid) {
This.gid = GID;
}
@Column (LENGTH=20)
Public String Getgaragenum () {
return garagenum;
}
public void Setgaragenum (String garagenum) {
This.garagenum = Garagenum;
}
The Cascadetype property has four values, where the Remove property is a cascade deletion, to achieve cascading deletion
Cascadetype.remove annotations must be added to the parent bar, which is the key to cascading deletes
@OneToMany (cascade={Cascadetype.remove},mappedby= "Garage")
Public set<auto> Getautos () {
return autos;
}
public void Setautos (Set<auto> autos) {
This.autos = Autos;
}
public void Addgarageauto (auto auto) {
Auto.setgarage (this);
This.autos.add (auto);
}
}
Auto.java
/**
* One to many multiple pairs of links
*/
Java code
Package com.jpa.bean1;
Import Javax.persistence.CascadeType;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import Javax.persistence.ManyToOne;
@Entity
public class Auto {
Private Integer autoid;
Private String Autotype;
Private String autonum;
Private garage garage;
@Id @GeneratedValue
Public Integer getautoid () {
return autoid;
}
public void Setautoid (Integer autoid) {
This.autoid = autoid;
}
Public String Getautotype () {
return autotype;
}
public void Setautotype (String autotype) {
This.autotype = Autotype;
}
Public String Getautonum () {
return autonum;
}
public void Setautonum (String autonum) {
This.autonum = Autonum;
}
@ManyToOne ()
@JoinColumn (name= "Garageid")
Public Garage getgarage () {
return garage;
}
public void Setgarage (garage garage) {
This.garage = garage;
}
}
Persistent data prior to cascading delete operations
Mysql> select * from garage;
+-----+-----------+
| GID | Garagenum |
+-----+-----------+
| 1 | Room1 |
| 2 | Room2 |
| 3 | Room3 |
+-----+-----------+
Mysql> select * from Auto;
+--------+---------+----------+----------+
| autoid | Autonum | Autotype | Garageid |
+--------+---------+----------+----------+
| 1 | hk2222 | Car | 1 |
| 2 | bj0000 | Car | 1 |
| 3 | jn1d31 | Bus | 3 |
| 4 | sh3243 | Car | 3 |
+--------+---------+----------+----------+
JUnit test Method Delete ()
Java code
@Test public void Delete () {
Entitymanagerfactory factory = persistence.createentitymanagerfactory ("Jpa-hibernate");
Entitymanager em = Factory.createentitymanager ();
Em.gettransaction (). Begin ();
Garage garage = Em.find (Garage.class, 3);
Em.remove (garage);
Em.gettransaction (). commit ();
Em.close ();
Factory.close ();
}
@Test public void Delete () {
Entitymanagerfactory factory = persistence.createentitymanagerfactory ("Jpa-hibernate");
Entitymanager em = Factory.createentitymanager ();
Em.gettransaction (). Begin ();
Garage garage = Em.find (Garage.class, 3);
Em.remove (garage);
Em.gettransaction (). commit ();
Em.close ();
Factory.close ();
}
The call to the Delete method is to implement cascading deletions, at which point the 3-per-gid field in table garage is deleted, and the fields that are Garageid 3 of the garage associated table auto are deleted.
Persistent data after cascading deletion
Mysql> select * from Auto;
+--------+---------+----------+----------+
| autoid | Autonum | Autotype | Garageid |
+--------+---------+----------+----------+
| 1 | hk2222 | Car | 1 |
| 2 | bj0000 | Car | 1 |
+--------+---------+----------+----------+
Mysql> select * from Auto;
+--------+---------+----------+----------+
| autoid | Autonum | Autotype | Garageid |
+--------+---------+----------+----------+
| 1 | hk2222 | Car | 1 |
| 2 | bj0000 | Car | 1 |
+--------+---------+----------+----------+