Hibernate EnVers中的@NotAudited和RelationTargetAuditMode.NOT_AUDITED有什么区别? [英] What's the difference between @NotAudited and RelationTargetAuditMode.NOT_AUDITED in Hibernate EnVers?

查看:204
本文介绍了Hibernate EnVers中的@NotAudited和RelationTargetAuditMode.NOT_AUDITED有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @NotAudited 
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@OneToMany(mappedBy =booking)
@OrderBy(bookingOrder )
私人列表< CustomerBooking> customerBookingList = new LinkedList< CustomerBooking>();

为什么要同时使用?

解决方案

如果您不想使用值/关系,请在字段中使用NotAudited根本不需要审计。我相信你可以在有或没有关系的领域如OneToMany,ManyToMany或者Column上使用它。如果您希望值被审计,而不是关系另一端的实体,请在关系字段上使用RelationTargetAuditMode.NOT_AUDITED。例如,您希望审核ID /键值,但不需要相关表格。

您也可以将RelationTargetAuditMode应用于整个类,我相信只是说类中的所有关系都不会审计另一端。这使我感到困惑,因为我错误地使用这个注释来表示不要审核下面的实体,这不是它的意思。如果您不希望实体审计,那么根本就没有对实体类的审计注释。在涉及实体的其他审计实体中,您必须将NotAudited或RelationTargetAuditMode.NOT_AUDITED用于关系字段。



官方文档对于此主题并不太好( http://docs.jboss.org/hibernate/orm/4.2 /devguide/en-US/html/ch15.html ),甚至根本没有提到NotAudited。



在我以前的项目中需要审核一组非常具体的表格而不是其他人,所以我需要使用这些注释。我与一些审计实体的非审计实体有外部关系。我经常使用RelationTargetAuditMode.NOT_AUDITED注释,至少我会审核外键值/ ID,而不是关系另一端的实体。如果你没有这个注释,你会得到一个运行时异常,其中ENVERS试图将审计记录插入到未审计实体的审计表中,并且该表不存在。我使用NotAudited注释来处理一些ManyToMany连接表关系,我只是不需要进行审计,并且审计实体表本身没有任何记录(无外键ID /值)。

哦,是的 - 文档没有说如果两者都使用(不确定哪一个优先),但是我不认为同时在给定字段上同时使用这两种方法会发生什么。使用一个或另一个。


@NotAudited
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@OneToMany(mappedBy = "booking")
@OrderBy("bookingOrder")
private List<CustomerBooking> customerBookingList = new LinkedList<CustomerBooking>();

Why use both? is it good to use both or would one suffice?

解决方案

Use NotAudited on fields when you don't want the value / relationship to be audited at all. I believe you can use this on a field with or without a relationship such as OneToMany, ManyToMany, or just Column. Use RelationTargetAuditMode.NOT_AUDITED on a relationship field if you want the value to be audited, but not the entity on the other side of the relationship. For example you want the ID / key value audited, but not the related table.

You can also apply the RelationTargetAuditMode to the entire class, which I believe just says for all relationships in the class don't audit the other end. This confused me as I was mistakenly using this annotation to mean don't audit the entity below, which is not what it means. Just don't have an Audit annotation on an entity class at all if you don't want the entity audited. On other audited entities that refer to the entity you'll have to either use NotAudited or RelationTargetAuditMode.NOT_AUDITED to the relationship field.

The official documentation is not great about this topic (http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch15.html) and doesn't even mention NotAudited at all.

In my past projects I've had a need to audit a very specific set of tables and not others so I needed to use these annotations. I have foreign key relationships to some of my non-audited entities from some audited entities. I often use the RelationTargetAuditMode.NOT_AUDITED annotation so that at least I audit the foreign key value / ID, just not the entity on the other end of the relationship. If you don't have this annotation you'll get a runtime exception where ENVERS tries to insert an audit record into an audit table for the non-audited entity and that table won't exist. I use the NotAudited annotation for a few ManyToMany join table relationships that I just don't need to audit and there is nothing on the audited entity table itself to record (no foreign key ID / value).

Oh yeah - the docs don't say what happens if you use both (not sure which one has priority), but I don't think using both simultaneously on a given field is intended. Use one or the other.

这篇关于Hibernate EnVers中的@NotAudited和RelationTargetAuditMode.NOT_AUDITED有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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