Hibernate:如何确保父项在其所有子实体都不被删除时? [英] Hibernate: How to ensure that a parent is not deleted when all its child entities are?

查看:184
本文介绍了Hibernate:如何确保父项在其所有子实体都不被删除时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在删除 entities ,因此我的数据库中有行。

I do >想要删除某个实体及其所有行。但是,我不希望从其中删除​​任何行。



我可以做到这一点吗?

狗窝父实体 Dog 是我要删除的实体。



请参阅下面的代码,了解我如何将2与Kennel实体相关联:

  @OneToMany(cascade = CascadeType.MERGE,orphanRemoval = false)
@JoinColumn(name =KENNEL_ID,referencedColumnName =ID,updatable = true,insertable = true)
private设置<狗和GT;小狗;

目前,当我删除dog entitie(s)时,其相关的Kennel实体 em>也被删除。



编辑:狗到狗窝的映射:

  @ManyToOne( cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name =KENNEL_ID)
private kennel kennel;


解决方案


删除dog entitie(s),其相关的犬舍实体也被删除。

原因是您有 cascade = CascadeType.ALL 设置在 ManyToOne 注释中。有了这个,我们告诉ORM,当我们删除(或任何其他操作) Dog 时,它应该传播相同的操作到 Kennel 实体。

 删除ManyToOne中的级联属性(cascade = CascadeType.ALL)。 




我可以保持在犬舍中显示的@oneToMany关系相同吗?


您可能需要考虑的变化很少。


  • 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>

I am deleting entities and hence rows from my database.

I do want to delete a certain entity and all of its child rows. However, I do not want to delete any rows from its Parent.

How can I accomplish this?

Kennel is the Parent Entity and Dog is the entity I am deleting.

Please see code below to how I have linked the 2 in the Kennel Entity:

@OneToMany(cascade = CascadeType.MERGE, orphanRemoval = false)
    @JoinColumn(name = "KENNEL_ID", referencedColumnName = "ID", updatable = true, insertable = true)
    private Set<Dog> dogs;

Currently, when I delete dog entitie(s), its related Kennel entity is also being deleted.

EDIT: Mapping of Dog to Kennel:

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "KENNEL_ID")
    private kennel kennel;

解决方案

Currently, when I delete dog entitie(s), its related Kennel entity is also being deleted.

The reason being you have 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 ).

Can I keep the @oneToMany relationship shown in kennel the same?

Few changes you might want to consider.

  • No need of having JoinColumn annotation at both oneToMany and ManyTone side.
  • Consider using 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.
  • Regarding 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.
  • Not sure if you have already implemented scaffolding code/methods to add/update the 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.

这篇关于Hibernate:如何确保父项在其所有子实体都不被删除时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆