Search code examples
javahibernate-envers

Is it possible to manually create Envers auditing tables without the revinfo foreign key?


In my current project I'm using Envers 5.2.12 to audit an entity changes. Because of security restrictions I must create manually the audit table and revinfo table and they cannot contain a foreign key. If I create them without the foreign key reference will that interfere in Envers normal behaviour?


Solution

  • The short answer, Envers will work with no foreign keys defined.

    The main caveat here is that this all assumes that the data being maintained remains consistent between related rows as if the Foreign Key was defined.

    In the event that audit data is inserted or deleted from the audit tables that would otherwise normally invalidate a Foreign Key and generate a Constraint Violation exception, all guarantees are voided.

    The purpose of a Foreign Key is to guarantee data integrity between related rows. Without them, you'll never be aware when an operation violates that directive and by the time you notice the problem your data integrity could be so compromised it may be better simply to start from scratch.

    While Envers (and generally speak most applications) can operate without FK definitions, you should use them as they're literally a corner stone principle in any relational database environment.