Can I create Hibernate Envers specific tables using Liquibase

旧街凉风 提交于 2019-12-05 05:59:50

I use in our project Hibernate, Envers and Liquibase.

The solution to add envers to the entity ExampleEntitity with db table exampleEntitity (only table, if it is simple entity and we don't audit relations):

First, add to liquibase <changeSet> tags eg.:

<changeSet author="Gal" id="createExampleEntitity_AUD">
    <createTable tableName="exampleEntitity_AUD">
        <column name="id" type="BIGINT">
            <constraints nullable="false"/>
        </column>
        <column name="REV" type="BIGINT">
            <constraints nullable="false"/>
        </column>
        <column name="REVTYPE" type="SMALLINT"/>
        (...)
    </createTable>
</changeSet>

<changeSet author="Gal" id="primaryKeyExampleEntitity_AUD">
    <addPrimaryKey columnNames="id, REV" tableName="exampleEntitity_AUD"/>
</changeSet>

<changeSet author="Gal" id="fkExampleEntitity_AUD_revisionsTable">
    <addForeignKeyConstraint baseColumnNames="REV" baseTableName="exampleEntitity_AUD" 
        constraintName="FK_revisions_exampleEntitity_AUD" 
        deferrable="false" initiallyDeferred="false" 
        onDelete="NO ACTION" onUpdate="NO ACTION" 
        referencedColumnNames="id" referencedTableName="revisionsTable"/>
</changeSet>

id, (...) -> fields from table exampleEntitity, which you want to audit.

Second, add @Audited adnotation to entity ExampleEntitity (or @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) if you dont want to audit related entities).

That's all. Now all changes of instances ExampleEntitity will be stored in table exampleEntitity_AUD.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!