如何在 Hibernate 中对关联映射 table 执行软删除?
How to perform soft deletes on an association mapping table in Hibernate?
因此,虽然我个人讨厌软删除,但我在一个每个 table 都必须只能软删除的项目中工作。我不确定如何处理关联 table 上的软删除,其字段如下所示:
@ManyToMany(targetEntity = AdvertisementVendor.class, fetch = FetchType.EAGER)
@JoinTable(name = "advertisement_version_advertisement_vendor_association",
joinColumns = @JoinColumn(name = "advertisement_version_id"),
inverseJoinColumns = @JoinColumn(name = "advertisement_vendor_id"))
private Set<AdvertisementVendor> _advertisement_vendors = new HashSet<>();
我已经了解如何做 soft deletes,但我不确定如何将其应用到协会 table。
更新:
采纳 Dragan Bozanovic 的建议,我将我的专栏更新为:
@ManyToMany(targetEntity = AdvertisementVendor.class, fetch = FetchType.EAGER)
@JoinTable(name = "advertisement_version_advertisement_vendor_association",
joinColumns = @JoinColumn(name = "advertisement_version_id"),
inverseJoinColumns = @JoinColumn(name = "advertisement_vendor_id"))
@WhereJoinTable(clause = "is_deleted = 0")
@SQLDelete(sql = "UPDATE advertisement_version_advertisement_vendor_association SET is_deleted = 1 WHERE advertisement_version_id = ? AND advertisement_vendor_id = ?", check = ResultCheckStyle.COUNT)
@SQLInsert(sql = "INSERT INTO advertisement_version_advertisement_vendor_association " +
"(advertisement_version_id, advertisement_vendor_id, is_deleted) VALUES(?, ?, 0) " +
"ON DUPLICATE KEY UPDATE is_deleted = 0")
private Set<AdvertisementVendor> _advertisement_vendors = new HashSet<>();
但这似乎不起作用。它似乎忽略了@SQLDelete 并只是删除了映射。
更新 2:
忽略第一个更新,它与不同的代码有关。上面的例子按原样工作。
您可以使用 @WhereJoinTable 来过滤连接表的条件:
Where clause to add to the collection join table. The clause is
written in SQL. Just as with Where
, a common use case is for
implementing soft-deletes.
我遇到了类似的问题,虽然您的解决方案确实有效,但除了@SQLDelete 之外,我还必须添加一个@SQLDeleteAll 注释。
我遇到的问题是,当我从 HashSet 中清除所有条目或删除父对象时,它仍在调用默认删除语句。
抱歉,如果我的术语有点偏差,我对 Hibernate 还是很陌生。
因此,虽然我个人讨厌软删除,但我在一个每个 table 都必须只能软删除的项目中工作。我不确定如何处理关联 table 上的软删除,其字段如下所示:
@ManyToMany(targetEntity = AdvertisementVendor.class, fetch = FetchType.EAGER)
@JoinTable(name = "advertisement_version_advertisement_vendor_association",
joinColumns = @JoinColumn(name = "advertisement_version_id"),
inverseJoinColumns = @JoinColumn(name = "advertisement_vendor_id"))
private Set<AdvertisementVendor> _advertisement_vendors = new HashSet<>();
我已经了解如何做 soft deletes,但我不确定如何将其应用到协会 table。
更新:
采纳 Dragan Bozanovic 的建议,我将我的专栏更新为:
@ManyToMany(targetEntity = AdvertisementVendor.class, fetch = FetchType.EAGER)
@JoinTable(name = "advertisement_version_advertisement_vendor_association",
joinColumns = @JoinColumn(name = "advertisement_version_id"),
inverseJoinColumns = @JoinColumn(name = "advertisement_vendor_id"))
@WhereJoinTable(clause = "is_deleted = 0")
@SQLDelete(sql = "UPDATE advertisement_version_advertisement_vendor_association SET is_deleted = 1 WHERE advertisement_version_id = ? AND advertisement_vendor_id = ?", check = ResultCheckStyle.COUNT)
@SQLInsert(sql = "INSERT INTO advertisement_version_advertisement_vendor_association " +
"(advertisement_version_id, advertisement_vendor_id, is_deleted) VALUES(?, ?, 0) " +
"ON DUPLICATE KEY UPDATE is_deleted = 0")
private Set<AdvertisementVendor> _advertisement_vendors = new HashSet<>();
但这似乎不起作用。它似乎忽略了@SQLDelete 并只是删除了映射。
更新 2:
忽略第一个更新,它与不同的代码有关。上面的例子按原样工作。
您可以使用 @WhereJoinTable 来过滤连接表的条件:
Where clause to add to the collection join table. The clause is written in SQL. Just as with
Where
, a common use case is for implementing soft-deletes.
我遇到了类似的问题,虽然您的解决方案确实有效,但除了@SQLDelete 之外,我还必须添加一个@SQLDeleteAll 注释。
我遇到的问题是,当我从 HashSet 中清除所有条目或删除父对象时,它仍在调用默认删除语句。
抱歉,如果我的术语有点偏差,我对 Hibernate 还是很陌生。