Search code examples
javahibernatehibernate-envers

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

Solution

  • 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;
        ...
    }