Hibernate:如何仅获取非逻辑删除的对象 [英] Hibernate: how to fetch only not-logically deleted objects

查看:19
本文介绍了Hibernate:如何仅获取非逻辑删除的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们数据库中几乎每个表都有一个到审计表的 FK,它记录了创建、更新和删除的状态(日期和用户名).

Nearly every table in our database has a FK to the Auditing table which logs created, updated and deleted status (date and username).

我们将审计表映射到审计类并像这样使用它:

We mapped the auditing table to the Auditing class and use it like this:

@MappedSuperclass
public class BusinessObject extends DataObject {

    private static final long serialVersionUID = -1147811010395941150L;

    @OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
    @JoinColumn(name = "AUD_ID")
    private AuditingObject auditing;
...

如您所料,几乎每个实体都源自 BusinessObject.

As you'd expect, nearly every entity extends from BusinessObject.

有没有一种简单的方法可以说,对于每个业务对象,只收到auditing.deleted is null".

Is there an easy way of saying, for every businessObject, only receive "auditing.deleted is null".

我已经尝试在 businessObject 中添加 @Where 和 @WhereJoinTable ,但这似乎不像我期望的那样工作.

I've tried adding a @Where and @WhereJoinTable in the businessObject but this doesn't seem to work as I expect.

目前,我已经对我的一个查询执行了此操作,并且此操作有效,但由于我们有大约 150 个查询,因此我不想对所有查询执行此操作.

Currently, i've done this to one of my queries and this works, but I'd hate to do this for all queries since we have about 150.

@NamedQuery(
    name="allCountries",
    query="SELECT c FROM Country c"
        + " LEFT JOIN FETCH c.labelDefinition "
        + " LEFT JOIN FETCH c.labelDefinition.translations "
        + " WHERE c.auditing.deleted is null"
        + " ORDER BY c.code"
)

推荐答案

IMO,实现软删除的最简单方法是在您的实体中添加一个标志并使用:

IMO, the easiest way to implement a soft-delete would be to add a flag in your entities and to use:

  • @SQLDelete 注释覆盖默认的 Hibernate delete(并执行标志的更新)
  • @Where(或 @Filters?) 注释您的实体和关联以过滤已删除的实体

不确定这如何与您的 Auditing 表相适应.还需要一些进一步的探索和测试.

Not sure how this can fit with your Auditing table though. Some further exploration and testing are required.

这篇关于Hibernate:如何仅获取非逻辑删除的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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