AidenPearce
AidenPearce

Reputation: 93

Hibernate Envers auditing non audited entities

I have several classes in my project which are handled by Hibernate, some are audited by Envers, some are not. Now, when I try to save a certain non-audited entity, I get this:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: audit_etc_etc)

Some could probably think that I just don't have the audit-table in my database, but Envers shouldn't even try to look for this table, because the entity is not audited. My classes look like this:

@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;
}

So each class holds a list of children, which have a reference to its parent. None of these classes are marked with the @Audited-annotation, but they have references to some audited entities. Yet each of these references is marked with the @Audited(targetAuditMode = RelationTargetAuditMode. NOT_AUDITED)-annotation.

I also found nothing out of the ordinary in my hibernate.cfg.xml-file, just the connection-details, envers-options, class-mappings and some other hibernate-options.

What is wrong here and how can I fix this problem occuring on save?

(Note: I only tested this for the classes A and B for now, but I assume that trying to save an instance of C will throw the same exception)

Update:
Enabling show_sql shows this when I try to save a B instance:

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 (?, ?, ?, ?)

Upvotes: 4

Views: 8124

Answers (3)

Naros
Naros

Reputation: 21153

The problem is your placement of the @Audited annotation to mark a non-audited relation. Rather than putting that annotation in your entities A, B, and C you should be placing those inside your entity E on the relationships to A, B, and C.

In other words:

@Entity
@Audited
public class E {
  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
  private A a;
  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
  private B b;
  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)     
  private C c;
}

Hope that helps.

Upvotes: 2

Ricardo Vila
Ricardo Vila

Reputation: 1632

The problem is that you mark yout attributes as @Audited so envers try to audit them as part of the Class it belongs, and the class intself. You should only mark with @Audited your classes declaration and the attributes inside them that you want to audit:

@Audited
@Entity
public class AuditedEntity{
  ...
}

and your not audited classes

@Entity
class A {
    AuditedEntity e;
    ...
}

If you use AuditedEntity inside another audited entity:

@Entity
@Audited
public class AnotherAuditedEntity {
    @Audited
    AuditedEntity e;
    ...
}

Upvotes: 3

AidenPearce
AidenPearce

Reputation: 93

As it turns out, the @Audited(...) causes Envers to audit the entities and thereby trying to write to non-existing audit-tables. Adding @NotAudited to every @Audited(..) solved the problem for me, maybe you don't need both annotations at all.

Upvotes: 1

Related Questions