4

I need to create Audit tables automatically when I saved to a table. I added "org.hibernate:hibernate-envers:5.4.2". Final to my gradle file and annotated the Entity using @Audited.

I cannot use "spring.jpa.hibernate.ddl-auto" in my application file and I need another way to create tables automatically. Like writing a gradle task or Java Code.

How I can get those table automatically created in the db.

RKRK
  • 1,284
  • 5
  • 14
  • 18
user9353766
  • 75
  • 12

2 Answers2

0

You can't auto generate table if you don't have hibernate.ddl-auto set to true. As @Uwe Allner suggested, you should use flyway/liquibase. They integrate rather easily with Spring. If you use them, each time you will launch your application, every sql script you needed to be executed will be.

You will create your audit table through these source control database. You will have something like this in your xml changelog file (liquibase version) in order to audit your table "table1":

<changeSet id="3" author="user9353766">
    <createTable tableName="audit_table1">
        <column name="ID" type="bigint">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="PROPERTY1" type="bigint">
            <constraints nullable="false"/>
        </column>
        <column name="REV" type="bigint"></column>
        <column name="REVTYPE" type="int"></column>
    </createTable>
</changeSet>
Pilpo
  • 1,236
  • 1
  • 7
  • 18
0

While Flyway and LiquiBase are likely the most used and known ways to deal with migrating database deltas across environments, they aren't the only choices, see Hibernate Tools.

The Hibernate Tools library allows you to use build time schema generation and write that schema delta to a file that you can then use and integrate as a part of your build or deployment.

Naros
  • 19,928
  • 3
  • 41
  • 71