首页 文章

带有liquibase的hibernate-envers

提问于
浏览
2

我的问题可能很简单,但是hibernate-envers的文档说我需要两个步骤来使hibernate envers工作:

  • 类路径上的hibernate-envers jar,

  • 实体上的@Audited注释 .

首先我补充说:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>5.2.12.Final</version>
</dependency>

其次,我在我的实体上面添加了@Audited注释:

@Entity
@Audited
@Table(name = "users")
public class User {...}

在这两个步骤后,我有一个选项:

  • 使用hbm2ddl工具生成自动架构

  • 单独创建架构 .

教程/文档中的很多人都说你应该单独做这个,所以这就是我的架构的样子(在百里香中) .

要手动创建模式,我必须使用一个特殊的表(通常称为revinfo)创建变更集:

<createTable tableName="revinfo">
        <column name="rev" type="integer">
            <constraints primaryKey="true"/>
        </column>
        <column name="revtstmp" type="bigint"></column>
    </createTable>

现在我只需要添加名为tablename_AUD的新表(ofc我可以通过在我的实体@AuditTable(“new_name”)中添加新注释来更改此名称,但事实并非如此)添加了rev和revtype列 .

这是我在liquibase中现有的一个表:

<createTable tableName="users">
        <column name="id" type="int">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="email" type="varchar(200)">
            <constraints unique="true" nullable="false"/>
        </column>
        <column name="first_name" type="varchar(60)">
            <constraints nullable="false"/>
        </column>
        <column name="last_name" type="varchar(60)">
            <constraints nullable="false"/>
        </column>
    </createTable>

所以这就是我的users_AUD的样子:

<createTable tableName="users_AUD">
        <column name="id" type="bigint">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="rev" type="bigint">
            <constraints referencedTableName="revinfo"
                         foreignKeyName="fk_users_AUD_revinfo"
                         referencedColumnNames="rev"
                         nullable="false"/>
        </column>
        <column name="revtype" type="smallint"/>
        <column name="email" type="varchar(200)">
            <constraints unique="true" nullable="false"/>
        </column>
        <column name="first_name" type="varchar(60)">
            <constraints nullable="false"/>
        </column>
        <column name="last_name" type="varchar(60)">
            <constraints nullable="false"/>
        </column>
    </createTable>

问题是:为什么我有错误:

org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义名称为'entityManagerFactory'的bean创建错误:init方法的调用失败;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / boot / registry / classloading / internal / ClassLoaderServiceImpl $ Work

我在我的pom中添加了实体管理器依赖,但它没有解决我的问题 .

早些时候称:

这样的映射是可能的,但必须使用@Audited(targetAuditMode = NOT_AUDITED)明确定义

@ThomasEdwin的回答帮助了我 . 他说我们需要

add @Audited to all related entities.

或者就像我注意到我们也可以在我们的实体中注释

@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED).

许多类似的问题已经过时(之前需要更多配置/设置envers,这就是我创建新问题的原因) .

更新:修复版本等终于应用程序运行,但当我尝试通过POST添加用户时,我有这样的错误:

https://pastebin.com/raw/d1vqY6xp

1 回答

  • 2

    这样的映射是可能的,但必须使用@Audited(targetAuditMode = NOT_AUDITED)明确定义

    您需要将 @Audited 添加到所有相关实体 .

    编辑:

    org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义名称为'entityManagerFactory'的bean创建错误:init方法的调用失败;嵌套异常是java.lang.NoClassDefFoundError:org / hibernate / boot / registry / classloading / internal / ClassLoaderServiceImpl $ Work

    这可能会有所帮助:Error creating bean with name 'entityManagerFactory' Invocation of init method failed . 您需要确保hibernate使用与@naros指出的版本相同的版本 .

相关问题