JPA 级联删除是指在实体之间建立级联关系,当删除一个实体时,会自动删除其关联的实体。JPA 的级联删除可以用来减少数据库中的冗余数据,从而提高数据库性能。
JPA 的级联删除可以通过 @OneToMany 或 @ManyToMany 注释来实现。@OneToMany 注释用于一对多的关系,@ManyToMany 注释用于多对多的关系。下面是一个使用 @OneToMany 来实现 JPA 级联删除的例子:
@Entity public class Parent { @Id private Long id; @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) private List<Child> children; // getters and setters... }
@Entity public class Child { @Id private Long id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") private Parent parent; // getters and setters... }
以下代码显示如何删除具有级联操作的相关实体。
下面的代码来自PersonDaoImpl.java。
package cn..common; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee emp = new Employee(); emp.setName("Tom"); emp.setId(1); Phone ph = new Phone(); ph.setEmployee(emp); ph.setNumber("1234567890"); ParkingSpace ps = new ParkingSpace(); ps.setEmployee(emp); ps.setLocation("Dept"); emp.setParkingSpace(ps); emp.getPhones().add(ph); em.persist(emp); emp = em.find(Employee.class, 1); em.remove(emp); } @PersistenceContext private EntityManager em; }
以下代码来自Phone.java。
package cn..common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Phone { @Id private long id; private String number; private String type; @OneToOne Employee employee; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String phoneNo) { this.number = phoneNo; } public String getType() { return type; } public void setType(String phoneType) { this.type = phoneType; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public String toString() { return "Phone id: " + getId() + ", no: " + getNumber() + ", type: " + getType(); } }
以下代码来自Employee.java。
package cn..common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.OneToOne; @Entity public class Employee { @Id private int id; private String name; @OneToOne(cascade={CascadeType.PERSIST, CascadeType.REMOVE}) @JoinColumn(name="PSPACE_ID") ParkingSpace parkingSpace; @OneToMany(cascade={CascadeType.PERSIST, CascadeType.REMOVE}, mappedBy="employee") Collection<Phone> phones; public Employee() { phones = new ArrayList<Phone>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public Collection<Phone> getPhones() { return phones; } public void setPhones(Collection<Phone> phones) { this.phones = phones; } public void setName(String name) { this.name = name; } public ParkingSpace getParkingSpace() { return parkingSpace; } public void setParkingSpace(ParkingSpace parkingSpace) { this.parkingSpace = parkingSpace; } public String toString() { return "Employee id: " + getId() + " name: " + getName() + " with " + getParkingSpace(); } }
下面的代码来自ParkingSpace.java。
package cn..common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="PARKING_SPACE") public class ParkingSpace { @Id private int id; private int lot; private String location; @OneToOne(mappedBy="parkingSpace") private Employee employee; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getLot() { return lot; } public void setLot(int lot) { this.lot = lot; } public String getLocation() { return location; } public void setLocation(String deptName) { this.location = deptName; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public String toString() { return "ParkingSpace id: " + getId() + " lot: " + getLot() + ", location: " + getLocation(); } }下载 Remove_With_Relationships_Cascade.zip
以下是数据库转储。
Table Name: EMPLOYEE Table Name: PARKING_SPACE Table Name: PHONE
JPA教程 -JPA查询命名示例命名查询是用于组织查询定义和提高应用程序性能的强大工具。命名查询是使用@NamedQuery注释定义的,它...
JPA教程 - JPA查询Not Empty示例JPA SQL可以检查属性是否为空。List l = em.createQuery(SELECT p FROM Project p WHERE p.e...
Lucene教程 -Lucene HelloWorld我们可以使用Lucene为您的应用程序添加全文搜索功能。索引我们将从一些字符串创建一个内存索引。S...
Java反射 - Java反射对象创建我们可以使用反射动态创建类的对象。通过调用其中一个构造函数。然后我们可以访问对象的字段的值,...