我的项目中有几个类由 Hibernate 处理,一些由 Envers 审计,一些则不是。现在,当我尝试保存某个未经审计的实体时,我得到了:
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: audit_etc_etc)
有些人可能认为我的数据库中没有审计表,但 Envers 甚至不应该尝试查找该表,因为该实体未经过审计。我的类(class)如下所示:
@Entity
class A {
/* some 'normal' attributes here */
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
AuditedEntity e;
List<B> listOfBs;
}
@Entity
class B {
/* more 'normal' attributes here */
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
AuditedEntity e; // and some more references to audited entities
A anA;
List<C> listOfCs;
}
@Entity
class C {
/* more 'normal' attributes here */
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
AuditedEntity e; // and some more references to audited entities
B anB;
}
所以每个类都有一个子类列表,这些子类都引用了它的父类。这些类都没有用 @Audited
注释标记,但它们引用了一些已审计的实体。然而,这些引用中的每一个都标有 @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
-注释。
我在 hibernate.cfg.xml 文件中也没有发现任何异常,只有连接详细信息、环境选项、类映射和其他一些 hibernate 选项。
这里有什么问题,我该如何解决保存时出现的这个问题?
(注意:我现在只对类 A 和 B 进行了测试,但我假设尝试保存 C 的实例将抛出相同的异常)
更新:
当我尝试保存 B 实例时,启用 show_sql
显示:
Hibernate: update table_b set all_attributes=? where idB=?
Hibernate: insert into audit_description (timestamp, description) values (?, ?)
Hibernate: insert into audit_table_b (revision_type, attributes, moreAttributes, revision) values (?, ?, ?, ?)
最佳答案
问题是您将您的属性标记为@Audited,因此envers 尝试将它们作为其所属类的一部分以及类本身进行审核。 您应该只用 @Audited 标记您的类声明和其中您要审核的属性:
@Audited
@Entity
public class AuditedEntity{
...
}
和你未审核的类(class)
@Entity
class A {
AuditedEntity e;
...
}
如果您在另一个被审计的实体中使用 AuditedEntity:
@Entity
@Audited
public class AnotherAuditedEntity {
@Audited
AuditedEntity e;
...
}
关于java - Hibernate Envers 审计非审计实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628212/