如果JPA要删除Orphans , 用 orphanRemoval=true ,
Deleting JPA Entity Objects
Existing entity objects can be deleted from the database either explicitly by invoking the remove method or implicitly as a result of a cascade operation.
This page covers the following topics:
Explicit Remove
In order to delete an object from the database it has to first be retrieved (no matter which way) and then in an active transaction, it can be deleted using the remove method:
Employee employee = em.find(Employee.class, 1);
em.getTransaction().begin();
em.remove(employee);
em.getTransaction().commit();
The entity object is physically deleted from the database when the transaction is committed. Embedded objects that are contained in the entity object are also deleted. If the transaction is rolled back and not committed the object is not deleted.
An IllegalArgumentException is thrown by remove if the argument is not a an instance of an entity class or if it is a detached entity. A TransactionRequiredException is thrown if there is no active transaction when remove is called because operations that modify the database require an active transaction.
Cascading Remove
Marking a reference field with CascadeType .REMOVE (or CascadeType .ALL , which includes REMOVE ) indicates that remove operations should be cascaded automatically to entity objects that are referenced by that field (multiple entity objects can be referenced by a collection field):
@Entity
class Employee {
:
@OneToOne(cascade=CascadeType.REMOVE)
private Address address;
:
}
In the example above, the Employee
entity class contains an address
field that references an instance of Address
, which is another entity class. Due to the CascadeType.REMOVE
setting, when an Employee
instance is removed the operation is automatically cascaded to the referenced Address
instance, which is then automatically removed as well. Cascading may
continue recursively when applicable (e.g. to entity objects that the Address
object references, if any).
Orphan Removal
JPA 2 supports an additional and more aggressive remove cascading mode which can be specified using the orphanRemoval element of the @OneToOne and @OneToMany annotations:
@Entity
class Employee {
:
@OneToOne(orphanRemoval=true)
private Address address;
:
}
When an Employee
entity object is removed the remove operation is cascaded to the referenced Address
entity object. In this regard, orphanRemoval=true
and cascade=CascadeType.REMOVE
are identical, and if orphanRemoval=true
is specified, CascadeType.REMOVE
is redundant.
The difference between the two settings is in the response to
disconnecting a relationship. For example, such as when setting the address
field to null
or to another Address
object.
-
If orphanRemoval=true
is specified the disconnected Address
instance is automatically removed. This is useful for cleaning up dependent objects (e.g. Address
) that should not exist without a reference from an owner object (e.g. Employee
).
-
If only cascade=CascadeType.REMOVE
is specified no automatic action is taken since disconnecting a relationship is not a remove operation.
To avoid dangling references as a result of orphan removal this
feature should only be enabled for fields that hold private non shared
dependent objects.
Orphan removal can also be set for collection and map fields. For example:
@Entity
class Employee {
:
@OneToMany(orphanRemoval=true)
private List<Address> addresses;
:
}
In this case, removal of an Address
object from the collection leads to automatic removal of that object from the database.
http://www.objectdb.com/java/jpa/persistence/delete
http://www.objectdb.com/java/jpa/persistence/crud
http://stackoverflow.com/questions/306144/jpa-cascadetype-all-does-not-delete-orphans ,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Orphan_Removal_.28JPA_2.0.29
分享到:
相关推荐
**Java Persistence API (JPA)** 是Java平台上的一个标准,用于管理关系数据库中的对象-关系映射(ORM)。它提供了一种方式,让开发者可以用面向对象的编程模型来操作数据库,而无需直接编写SQL语句。JPA允许你在...
通过阅读和研究JPA的源代码,开发者可以了解到JPA如何处理实体的生命周期,如加载、保存、更新和删除,以及如何执行JPQL查询和Criteria查询。此外,还可以了解JPA如何与不同的持久化提供者(如Hibernate、Eclipse...
2. **实体管理器(EntityManager)**:负责实体的创建、查找、删除等操作,是JPA的主要接口。 3. **实体管理器工厂(EntityManagerFactory)**:用于创建实体管理器,是整个应用的单例对象。 4. **事务(Transaction...
1. **实体管理器(EntityManager)**:这是JPA的核心接口,负责所有与实体类相关的操作,如持久化、删除、查找和事务管理。 2. **查询语言(JPQL)**:Java Persistence Query Language,一种类似于SQL的语言,用于...
本篇文章将深入探讨JPA中的对象加载、更新、删除操作以及如何使用JPQL(Java Persistence Query Language)进行查询,这些都是Java开发者在处理持久化层时不可或缺的知识点。 首先,我们来看JPA的对象加载。JPA提供...
5. 使用JPA进行CRUD(创建、读取、更新、删除)操作的示例。 6. JPQL查询语法和HQL(Hibernate Query Language)的区别与使用场景。 7. JPA的事务管理,包括自动和手动事务模式。 8. 性能优化技巧,如批处理、懒加载...
2. **Entity Management**:Spring Data JPA 提供了对实体(Entity)的管理,包括实体的创建、更新、删除等操作。开发者可以通过 `EntityManager` 和 `EntityManagerFactory` 进行这些操作,但 Spring Data JPA 提供...
Spring框架的核心特性包括依赖注入(DI)和面向切面编程(AOP),并且它还提供了对数据库操作的支持,这主要通过Spring Data JPA和Java Persistence API(JPA)实现。 Spring注解是Spring框架中的一大特色,它极大...
通过 Spring Data JPA,开发者几乎无需编写任何 DAO(数据访问对象)实现,即可完成常见的 CRUD(创建、读取、更新、删除)操作以及分页、排序等功能。Spring Data JPA 提供了一套灵活的查询机制,允许开发者通过...
例如,Spring Data JPA支持自动化的查询生成,只需定义Repository接口,无需编写任何实现代码,就可以执行CRUD(创建、读取、更新、删除)操作。此外,它还支持复杂的查询方法命名,如findByXXX,根据方法名自动构建...
JPA的事务管理是指对一系列操作的管理,包括创建、读取、更新和删除(CRUD)等操作。事务管理的目的是确保数据的一致性和完整性。 JPA的事务管理有两种方式:JTA(Java Transaction API)和RESOURCE_LOCAL。JTA是...
### JPA概述与核心知识点详解 #### 一、JPA概览 JPA,全称Java Persistence API,作为Java EE 5.0平台标准的ORM(Object-Relational Mapping)规范,旨在解决对象持久化问题,使开发人员能更轻松地在Java应用程序...
Java Persistence API (JPA) 是Java企业版5(Java EE 5)的一部分,与Enterprise JavaBeans(EJB)3.0规范一起,极大地简化了Java持久化。它提供了一种对象关系映射方法,允许您以标准、可移植的方式声明性地定义...
Spring Data JPA API。 Spring Data JPA 开发文档。 官网 Spring Data JPA API。
Gemini JPA是在OSGi环境下(特别是Eclipse RCP)中使用的Java持久化框架。OSGi是一个模块化服务平台,允许在Java环境中动态地安装、启动、停止和卸载组件,而不会影响其他组件的正常运行。在Java开发中,持久化层...
- 删除数据: 使用`remove()`方法。 **JPA查询语言(JPQL)** JPA提供了自己的查询语言——Java Persistence Query Language(JPQL),类似于SQL,但更面向对象。例如,以下JPQL查询返回所有用户: ```java String...
实体管理器负责与数据库交互,执行CRUD(创建、读取、更新、删除)操作。实体管理工厂是单例模式的工厂类,用于创建实体管理器实例。JPQL(Java Persistence Query Language)是JPA提供的面向对象的查询语言,类似于...
- **实体管理器(EntityManager)**:它是JPA的核心接口,用于执行CRUD(创建、读取、更新、删除)操作。 - **持久化上下文(Persistence Context)**:它是一个缓存区,存储了实体的状态,包括新实体、修改后的实体...
**Java 持久化 API(JPA)详解(一)** Java 持久化 API(Java Persistence API),简称 JPA,是 Java 平台上的一个标准,用于管理关系数据库中的数据。JPA 提供了一种面向对象的方式来操作数据库,使得开发人员...