Hibernate:如何确保父项在其所有子实体都不被删除时? [英] Hibernate: How to ensure that a parent is not deleted when all its child entities are?
问题描述
我正在删除 entities
,因此我的数据库中有行。
I do >想要删除某个实体及其所有子
行。但是,我不希望从其父
中删除任何行。
我可以做到这一点吗? 请参阅下面的代码,了解我如何将2与Kennel实体相关联: 目前,当我删除dog entitie(s)时,其相关的Kennel实体 em>也被删除。 编辑:狗到狗窝的映射: 删除dog entitie(s),其相关的犬舍实体也被删除。 原因是您有 我可以保持在犬舍中显示的@oneToMany关系相同吗? 您可能需要考虑的变化很少。 I am deleting I do want to delete a certain entity and all of its How can I accomplish this? Please see code below to how I have linked the 2 in the Kennel Entity: Currently, when I delete dog entitie(s), its related Kennel entity is also being deleted. EDIT: Mapping of Dog to Kennel:
Currently, when I delete dog entitie(s), its related Kennel entity is
also being deleted. The reason being you have
Can I keep the @oneToMany relationship shown in kennel the same? Few changes you might want to consider. 这篇关于Hibernate:如何确保父项在其所有子实体都不被删除时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
狗窝
是父实体
和 Dog
是我要删除的实体。
@OneToMany(cascade = CascadeType.MERGE,orphanRemoval = false)
@JoinColumn(name =KENNEL_ID,referencedColumnName =ID,updatable = true,insertable = true)
private设置<狗和GT;小狗;
@ManyToOne( cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name =KENNEL_ID)
private kennel kennel;
cascade = CascadeType.ALL
设置在 ManyToOne
注释中。有了这个,我们告诉ORM,当我们删除(或任何其他操作) Dog
时,它应该传播相同的操作到 Kennel
实体。
删除ManyToOne中的级联属性(cascade = CascadeType.ALL)。
oneToMany中不需要有
和 JoinColumn
注解 ManyTone
一边。
OneToMany
注释中使用 mappedBy =kennel
属性并删除 JoinColum
标注在 OneToMany
一侧。这使得 ManyToOne
拥有方,并且从持久化 kennel
实体时生成的SQL也更高效。您可以通过启用 show_sql
来自行检查。
OneToMany
中的 cascade
属性是否将其设置为 ALL
或 MERGE
或 PERSIST,MERGE
取决于父实体上的哪些操作您想传播给子实体。
oneToMany
关系。如果没有,那么实施它们是一个好主意,因为这可以确保关联在两端都得到更新。请参阅 scaffolding code if needed。 li>
entities
and hence rows from my database.child
rows. However, I do not want to delete any rows from its Parent
. Kennel
is the Parent Entity
and Dog
is the entity I am deleting. @OneToMany(cascade = CascadeType.MERGE, orphanRemoval = false)
@JoinColumn(name = "KENNEL_ID", referencedColumnName = "ID", updatable = true, insertable = true)
private Set<Dog> dogs;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "KENNEL_ID")
private kennel kennel;
cascade=CascadeType.ALL
set on ManyToOne
annotation. With this we are telling the ORM that when we delete (or any other operation) Dog
it should propagate the same operation to the Kennel
entity as well. Remove cascade attribute in ManyToOne(cascade = CascadeType.ALL ).
JoinColumn
annotation at both oneToMany
and ManyTone
side. mappedBy="kennel"
attribute in OneToMany
annotation and remove JoinColum
annotation on OneToMany
side. This makes ManyToOne
the owning side and is also more efficient from SQLs that gets generated when you persist kennel
entity. You can check it yourself by enabling show_sql
. cascade
attribute on OneToMany
whether to set it to ALL
or MERGE
or PERSIST, MERGE
depends on which operations on parent entity you want to propagate to child entity.oneToMany
relationship. If not, it is a good idea to implement them because that ensures the association is updated on both the ends. Refer to scaffolding code if needed.