Java 我可以使用Liquibase创建特定于Hibernate Envers的表吗

Java 我可以使用Liquibase创建特定于Hibernate Envers的表吗,java,spring,hibernate,hibernate-envers,Java,Spring,Hibernate,Hibernate Envers,我们的Java应用程序是基于Spring的,我们有域类和通过Liquibase生成的相应模式 我们计划增加对单个域的审计支持 a。我们没有hibernate.xml和hibernate.cfg.xml,而是使用application-context.xml。那么如何通过@Audited这样的注释创建审计表呢 我如何解决这个问题?我将hibernate配置添加为 <property name="hibernateProperties"> <props>

我们的Java应用程序是基于Spring的,我们有域类和通过Liquibase生成的相应模式

我们计划增加对单个域的审计支持

a。我们没有hibernate.xml和hibernate.cfg.xml,而是使用application-context.xml。那么如何通过@Audited这样的注释创建审计表呢

我如何解决这个问题?我将hibernate配置添加为

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.ejb.event.post-insert">org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.post-update">org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.post-delete">org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.pre-collection-update">org.hibernate.envers.event.AuditEventListener</prop>
                <!-- <prop key="hibernate.ejb.event.pre-collection-remove">org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.post-collection-recreate">org.hibernate.envers.event.AuditEventListener</prop> -->
                <prop key="org.hibernate.envers.revision_field_name">REV</prop>
                <prop key="org.hibernate.envers.revision_type_field_name">REVTYPE</prop>
                <prop key="org.hibernate.envers.auditTablePrefix"></prop>
                <prop key="org.hibernate.envers.auditTableSuffix">_AUD</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
但是这个配置没有在开发环境中创建审计表。不清楚我在这里缺少了什么额外的配置


b。如何使用Liquibase创建必要的特定于envers的模式,生产团队对在生产环境中自动生成SQL模式的想法并不满意。

我在项目Hibernate、envers和Liquibase中使用

使用db table
ExampleEntity
将envers添加到实体
ExampleEntity
的解决方案(仅表格,如果它是简单实体且我们不审核关系):

首先,添加到liquibase
标签,例如:

<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
(…)
->表ExampleEntity中要审核的字段

Second,将
@Audited
adnotation添加到实体
exampleentity
(或者
@Audited(targetAuditMode=RelationTargetAuditMode.NOT\u Audited)
,如果您不想审核相关实体)


就这些。现在,所有实例的更改都将存储在表
exampleentity\u AUD
中,您可以在使用enver时手动创建审计表。所以我想你需要在liquibase中添加配置来创建这些表。谢谢你的回复!!如果我手动创建审计表,那么在更新原始表中的任何字段时如何填充?您使用的是哪个Hibernate版本?对于4,您不需要任何特殊配置来启用审核,只需添加jar就足够了。我们使用的是hibernate版本:3.6.3。如果您使用的是hibernate>=4,则无需手动执行任何操作。@anujpradhan您能详细说明您的评论吗?Hibernate4是否自动生成审计表?
<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>