休眠:如何只提取非逻辑删除的对象 [英] Hibernate: how to fetch only not-logically deleted objects
问题描述
我们映射了审计表到审计类,并像这样使用它:
@MappedSuperclass
public class BusinessObject extends DataObject {
private static final long serialVersionUID = -1147811010395941150L;
@OneToOne(fetch = FetchType.EAGER,cascade = {CascadeType.ALL})
@JoinColumn(name =AUD_ID)
私人AuditingObject审计;
...
正如您所料,几乎每个实体都从BusinessObject扩展而来。 / p>
有没有一种简单的方法可以说,对于每个业务对象,只接收auditing.deleted为null。
我已经尝试在businessObject中添加@Where和@WhereJoinTable,但这看起来并不像我期望的那样工作。
目前,我已经完成了这个任务,并且这个工作很有效,但是我不想为所有查询做这件事,因为我们大约有150个。 b
$ $ $ $ $ $ c $ @ $> $ name $'code $ @ $ b +LEFT JOIN FETCH c.labelDefinition
+LEFT JOIN FETCH c.labelDefinition.translations
+WHERE c.auditing.deleted为空
+ORDER BY c .code
)
实施软删除的最简单方法是在实体中添加一个标志并使用: ://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-customsqlrel =noreferrer> 不确定这可以适合您的 @SQLDelete
注释来覆盖默认的Hibernate delete
(并执行标志的更新)
@Where
(或 @Filters
?)对您的实体和关联进行注释以过滤已删除的实体
Auditing
表。一些进一步的探索和测试是必需的。
资源
>
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;
...
As you'd expect, nearly every entity extends from BusinessObject.
Is there an easy way of saying, for every businessObject, only receive "auditing.deleted is null".
I've tried adding a @Where and @WhereJoinTable in the businessObject but this doesn't seem to work as I expect.
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, the easiest way to implement a soft-delete would be to add a flag in your entities and to use:
- the
@SQLDelete
annotation to override the default Hibernatedelete
(and perform an update of the flag) - the
@Where
(or@Filters
?) annotation on your entities and associations to filter the deleted entities
Not sure how this can fit with your Auditing
table though. Some further exploration and testing are required.
Resources
这篇关于休眠:如何只提取非逻辑删除的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!