Cascade deletion of JPA Onetomany cascading operations

Source: Internet
Author: User
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 |
+--------+---------+----------+----------+

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.