- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
假设我们有两个实体(Profile
和Address
)。一个配置文件可以有多个地址。在这种情况下,我们有:
Profile:
...
fields:
...
oneToMany:
addresses:
targetEntity: Address
mappedBy: profile
orphanRemoval: true
Address:
...
fields:
...
manyToOne:
profile:
targetEntity: Profile
inversedBy: addresses
joinColumn:
name: profile_id
referencedColumnName: id
onDelete: cascade
现在,如果我删除包含多个地址的配置文件:
$em->remove($profile);
地址如何被删除?
Doctrine 是获取与此配置文件相关的所有地址然后删除它们,还是只删除配置文件并让数据库处理地址?
我找到了一些关于 Hibernate 的答案,但没有找到关于 Doctrine 的答案。
编辑:添加Doctrine书中的三个注释
1.If an association is marked as CASCADE=REMOVE Doctrine 2 will fetch this association. If its a Single association it will pass this entity to
EntityManager#remove()
. If the association is a collection, Doctrine will loop over all its elements and pass them toEntityManager#remove()
. In both cases the cascade remove semantics are applied recursively. For large object graphs this removal strategy can be very costly.2.Using a DQL DELETE statement allows you to delete multiple entities of a type with a single command and without hydrating these entities. This can be very efficient to delete large object graphs from the database.
3.Using foreign key semantics onDelete="CASCADE" can force the database to remove all associated objects internally. This strategy is a bit tricky to get right but can be very powerful and fast. You should be aware however that using strategy 1 (
CASCADE=REMOVE
) completely by-passes any foreign keyonDelete=CASCADE
option, because Doctrine will fetch and remove all associated entities explicitly nevertheless.
最佳答案
我做一个测试:
我首先获取 Profile
(只有 profile
而没有 join )并将其传递给 $em->remove($profile)
,然后 doctrine 运行另一个查询,获取与 Profile
相关的所有 Address
(一个查询),然后是 doctrine 对与 Profile
相关的每个地址运行删除查询,最后删除 Profile
。
所以,我可以说 orphanRemoval 是另一种类型的级联,正如 Doctrine 所说:
orphanRemoval: There is another concept of cascading that is relevant only when removing entities from collections
和orphanRemoval
绕过onDelete
。
关于php - orphanRemoval 会绕过 onDelete 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29723041/
我有 2 个实体: 轨迹 引用资料 在每个Locus上我们可以定义很多不同的References,一个Reference可以被很多Locus使用。然后我有一个 ManyToMany 关系。 但是,在我
以下两个类(CD 和 DVD)彼此不相关。每个类都有不同的表。但都有子类,如地址。 保存 DVD/CD 的值时,条目插入到相应的表中。因此,当我尝试在 Address_Table 中添加值时,它使用
我在使用 hibernate 持久化克隆对象时遇到了问题。当它的嵌套子项被删除时,记录并没有从数据库中删除(我已经设置了 orphanRemoval = true)。 在下面的代码中,使用 json
我对双向 OneToOne 关系和孤儿删除有点困惑。这些是我的实体: @Entity @Table(name = "city") public class City { @Id @Ge
根据这篇文章Difference between @OneToMany and @ElementCollection?我应该更喜欢 @ElementCollection 用于可嵌入类型,而 @OneT
我有实体 Event 与另一个实体有特定关系: @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "even
我有一个 RECIPE 表,它与 INGREDIENT 表具有 OneToMany 关系,因为单个配方可以包含多种成分。问题是,如果用户删除一个成分(前端将所有字段(ingredient_id 和 i
我的目的是仅当实体连接音轨没有其他记录时才删除实体艺术家中的记录。 我尝试以这种方式使用 orphanRemoval: Soundtrack.php /** * @Assert\NotBlank(m
我到处搜索,但没有找到好的答案。 我正在使用 Hibernate + Spring + Mysql 开发一个 Maven 应用程序。 这是我的 application-context.xml :
我有非常具体的情况,并尝试了不同的场景来实现从Java集合中删除时从数据库中删除。 我的 House 与 Room 具有 OneToMany 关系,而 Room 与 Bed 具有 OneToMany
我一直在阅读有关 orphanRemoval= true 的帖子在 JPA 中。根据文档: orphanRemoval is a flag - Whether to apply the remove
我在 Hibernate 引用书的第 21 章中有一个基本的一对多父/子关系。 级联仅从子级到父级(持久级联只是因为我不想删除子级时删除父级)。 当我将一个 child 添加到 parent 并保存
以下示例已简化。 我有以下数据库结构: X -xid VARCHAR Y -yid VARCHAR -xid VARCHAR -zid VARCHAR Z -zid VARCHAR 我有以下
假设我们有两个实体(Profile 和Address)。一个配置文件可以有多个地址。在这种情况下,我们有: Profile: ... fields: ... o
我在使用 JPA/Hibernate (3.5.3) 设置时遇到问题,其中我有一个实体,一个“Account”类,它有一个子实体列表,“Contact”实例。我正在尝试能够将 Contact 的实例添
我有一个带有 fos restbundle 的 Symfony rest api 构建,我正在反序列化一个 json PUT 请求,以便更新具有多对多关系的学说实体。 但是,配置了orphanremo
因此,在我的一个实体中将 orphanRemoval = true 添加到 @OneToMany 关系后,在尝试保存新实体或删除现有实体时,出现以下异常:引用带有 orphanRemoval = tr
我正在使用 JPA 2.1、EclipseLink 2.5.0、SQLite3 数据库和 Swing 制作一个应用程序。 我有两个实体,EntityClient和EntityPhone ,其中第一个有
上面两个选项有什么区别?什么时候选择每个选项更合适? 最佳答案 它们之间的基本区别是: When using the orphanRemoval=true option Doctrine makes
我正在使用 hibernate 4.1.0,jpa 2.1。当我尝试建立一对多关系时,出现上述错误。我已经尝试过其他关于堆栈溢出的解决方案,但它们对我不起作用 这是我的 Bean 类: @Entity
我是一名优秀的程序员,十分优秀!