springspring-boothibernate-enversspring-data-envers

How to catch errors in Envers?


I have set my auto updating of tables to none

spring.jpa.properties.hibernate.hbm2ddl.auto=none

That way when I am running Envers I will have to create the audit tables on my own. However when I have set an entity to be audited with the @Audited annotation and I have not created an audit table for that entity I run into an error because Envers then tries to populate the audit table that does not exist. This error is crucial because it then breaks the process of updating/inserting/deleting the entity because of the audit breaking.

Is there any way to have some sort of try/catch for Envers such that if there is this sort of error it does not break the main process?

P.S.

I am also using a test database to create the audit tables automatically, but it runs at a set time or when called. I would still like some way to check for errors and in a way bypass the auditing if there is an error in case I forget to call the script or the test database updating of the tables fails.


Solution

  • This error is crucial because it then breaks the process of updating/inserting/deleting the entity because of the audit breaking.

    That's kinda the point here.

    When you define an entity mapped with @Audited you have specified that you want track changes to that entity and therefore if such changes cannot be tracked due to a missing table or column the transaction will be rolled back to maintain consistency state between the audit tables and the main entity table.

    In Hibernate 6, the idea there is we're actually considering introducing categorized HBM2DDL control where you can set none for your main entity tables and use update for Envers, which completely avoids this problem you face entirely since the point behind Envers is to shadow the main tables.

    For now you could simply set the hbm2ddl.auto configuration property to validate to at least report the problem earlier in your process rather than during runtime if a table is missing.