我可以使用Liquibase创建Hibernate Envers特定表吗? [英] Can I create Hibernate Envers specific tables using Liquibase

查看:179
本文介绍了我可以使用Liquibase创建Hibernate Envers特定表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我们计划添加对单个域进行审计的支持。

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



我该如何解决这个问题?我已经添加了hibernate配置,如

 < property name =hibernateProperties> 
<道具>
< 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>
< /道具>
< / property>

在我的域名类中添加了@Audited注释

  @Entity 
@Audited
@Table(name =user)
public class User实现Serializable {

但是这种配置并没有在开发环境中创建审计表。它不清楚什么额外的配置我在这里失踪。



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

解决方案在我们的项目中使用Hibernate,Envers和Liquibase。

将envers添加到实体的解决方案 ExampleEntitity with db table exampleEntitity (只有表,如果它是简单的实体并且我们不审计关系):

第一,添加到liquibase < changeSet> 标签例如:

 < changeSet author =Galid =createExampleEntitity_AUD> 
< createTable tableName =exampleEntitity_AUD>
< column name =idtype =BIGINT>
< constraints nullable =false/>
< / column>
< column name =REVtype =BIGINT>
< constraints nullable =false/>
< / column>
< column name =REVTYPEtype =SMALLINT/>
(...)
< / createTable>
< / changeSet>

< changeSet author =Galid =primaryKeyExampleEntitity_AUD>
< addPrimaryKey columnNames =id,REVtableName =exampleEntitity_AUD/>
< / changeSet>

< changeSet author =Galid =fkExampleEntitity_AUD_revisionsTable>
< addForeignKeyConstraint baseColumnNames =REVbaseTableName =exampleEntitity_AUD
constraintName =FK_revisions_exampleEntitity_AUD
deferrable =falseinitialDeferred =false
onDelete =NO ACTION onUpdate =NO ACTION
referencedColumnNames =idreferencedTableName =revisionsTable/>
< / changeSet>

id (...) - >来自表 exampleEntitity 的字段,您要审核它。 第二,将 @Audited adnotation添加到实体 ExampleEntitity (或 @Audited( targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)如果您不想审计相关实体)。



就是这样。现在实例 ExampleEntitity 的所有更改都将存储在表 exampleEntitity_AUD 中。


Our Java app is Spring based and we have domain classes and the corresponding schema generated via Liquibase.

We are planning to add support for a single domain to be audited.

a. We don't have hibernate.xml and hibernate.cfg.xml instead we are using application-context.xml. Then how do I create audit table through annotations like @Audited.

How do I solve this issue? I have added hibernate configuration as

<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>

Added @Audited annotation in my domain class

@Entity
@Audited
@Table(name="user")
public class User implements Serializable {

But this configuration did not create audit tables in the development environment. Its not clear what additional configuration I am missing here.

b. How should I create the necessary envers specific schema using Liquibase, the production team is not comfortable with the idea of auto generating the SQL schema as well in the production environment.

解决方案

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.

这篇关于我可以使用Liquibase创建Hibernate Envers特定表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆