0

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.

emcho
  • 35
  • 4

1 Answers1

0

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.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • Thank you for the response. A seperate HBM2DDL control for Envers would certainly solve my problem. Is there anyway for me to go into the Envers source code and modify anything to check whether the audit table exists before insertion? And if not then continue rather than throw an error. I want my Auditing implementation to be hands-free after I add the @Audited annotation to an entity. – emcho Nov 25 '19 at 23:28
  • Anything is possible, the question is whether its practical. Checking the existence of a table for every entity insert/update/remove event will be significantly costly on performance, so I would advise against this. Otherwise, you'd need to extend all the Envers event listeners and add such logic or attempt to cache information about this during bootstrap as an integrator and then check this shared state during each transaction. But again, I strongly advise against this. – Naros Nov 26 '19 at 14:11
  • I see. I will have to think about this more, but thank you for your insight. Hopefully Hibernate 6 has this patch :). – emcho Nov 26 '19 at 18:03