I am building a spring boot application. In src/main/resources
I have a schema.sql
file which will be automatically executed by spring to create my database schema.
Recently I added Auditing with Hibernate envers. Of course I do not want to add the _AUD
tables manually in my schema.sql.
With an embedded database like h2 I was simply able to write in application.properties
hibernate.hbm2ddl.auto=create-drop
so that the tables was generated automatically.
After switching to a "real" database the property has lost its effect. Besides this property is just conveniece and not recommended for production.
So the right solution is to let hibernate create the audit tables automatically. But how can I do this? I want ONLY auto-generate the schema for the auditing tables?
Unfortunately, there isn't a separation between sources when it comes to Hibernate 5's schema management functionality; it's simply an all or nothing toggle.
You could look into using the Hibernate Tooling Schema Generation Ant task in order to produce the schema creation script and then manually modify it to fit your needs. You may even need to manually execute it if you cannot find a way to inject it into your bootstrap setup for your first execution.
The good news is that Hibernate 6 will introduce a way that will allow you to do exactly what you ask. You'll be able to allow Hibernate ORM's entities to perhaps be specified as validate
while the audit table entities can be specified as update
or any other setting. The Hibernate 6 schema management tooling will be able to differentiate these various sources of schema objects and manage them in a more independent means.
But until Hibernate 6, you'll have to rely on the tooling task to generate schemas offline and likely do some manual manipulation of those scripts for inclusion based on your needs.