Deleting JPA Entity Objects
2015-08-28 16:55
495 查看
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
Cascading Remove
Orphan Removal
DELETE Queries
remove method:
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.
(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):
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).
orphanRemoval element of the
@OneToOne and
@OneToMany annotations:
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:
In this case, removal of an Address object from the collection leads to automatic removal of that object from the database.
Queries in JPA/JPQL in chapter 4 explains how to use JPA DELETE queries.
原文链接:http://www.objectdb.com/java/jpa/persistence/delete
Orphan Removal= true有两个功能
1.和Cascade.REMOVE一样,删除主动方, 被动方也删除
2.当为true的时候, @OneToOne, @OneToMany关系中的被动方,和主动方失去联系的时候, 比如, Mike搬家了,地址改了, 那么, 就会这样
Mike.setAddress("New Address"), 那么原来的老地址就没有用了,自动从数据库中删除这个孤儿, Orphan的意思就是孤儿。 这个功能一般用在非常专属的属性上
remove method or implicitly as a result of a cascade operation.
This page covers the following topics:
Explicit Remove
Cascading Remove
Orphan Removal
DELETE Queries
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 theremove method:
Employee employee = em.findfind(entityClass, primaryKey)EntityManager's methodFind by primary key. See JavaDoc Reference Page...(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 theorphanRemoval 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.
DELETE Queries
DELETE queries provide an alternative way for removing entity objects from the database. Deleting objects using a DELETE query may be useful especially when many entity objects have to be deleted in one operation. The DELETEQueries in JPA/JPQL in chapter 4 explains how to use JPA DELETE queries.
原文链接:http://www.objectdb.com/java/jpa/persistence/delete
Orphan Removal= true有两个功能
1.和Cascade.REMOVE一样,删除主动方, 被动方也删除
2.当为true的时候, @OneToOne, @OneToMany关系中的被动方,和主动方失去联系的时候, 比如, Mike搬家了,地址改了, 那么, 就会这样
Mike.setAddress("New Address"), 那么原来的老地址就没有用了,自动从数据库中删除这个孤儿, Orphan的意思就是孤儿。 这个功能一般用在非常专属的属性上
相关文章推荐
- GSON Empty Json Object to null
- Objective-C语法之代码块(block)的使用
- Objective-C代码规范(1)
- iOS js与objective-c的交互(转)
- 字典方法setValue:ForKey setObject:ForKey的区别
- 原生js的Function,Array,Object构造函数的prototype原型方法扩展
- iOS开发之Objective-C语法之词典对象
- Q_OBJECT宏的作用
- QT:线程、事件与QObject
- java.lang.IllegalStateException: attempt to re-open an already-closed object
- [转]java中JSONObject与JSONArray的使用详细说明及有关JSON的工具类
- [转]JSONObject与JSONArray的使用
- [转]json+JSONObject+JSONArray 结合使用
- [转] json字符串转换为JSONObject和JSONArray
- Object类
- 黑马程序员——ios基础---Objective-C:Foundation
- Objective-C 学习笔记:从HelloWorld开始
- 条款4:确定对象被使用前已被初始化(Make sure that objects are initialized before they're used)
- 'WSGIRequest' object has no attribute 'user'
- Objective-C 【关于导入类(@class 和 #import的区别)】