使用 liquibase 的 hibernate-envers

deg*_*ath 6 java spring hibernate hibernate-envers

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

  1. 类路径上的 hibernate-envers jar,

  2. 实体上的@Audited 注释。

首先我补充说:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>5.2.12.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

@Entity
@Audited
@Table(name = "users")
public class User {...}
Run Code Online (Sandbox Code Playgroud)

在这两个步骤之后,我可以选择:

  1. 使用 hbm2ddl 工具生成自动模式

  2. 单独创建模式。

教程/文档等中的很多人都说你应该单独做,所以这就是我的模式的样子(在百里香叶中)。

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

    <createTable tableName="revinfo">
        <column name="rev" type="integer">
            <constraints primaryKey="true"/>
        </column>
        <column name="revtstmp" type="bigint"></column>
    </createTable>
Run Code Online (Sandbox Code Playgroud)

现在我只需要添加名为 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>
Run Code Online (Sandbox Code Playgroud)

所以这就是我的 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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

之前叫:

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

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

add @Audited to all related entities.
Run Code Online (Sandbox Code Playgroud)

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

@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED).
Run Code Online (Sandbox Code Playgroud)

许多类似的问题已经过时了(早些时候需要配置/设置环境,这就是我创建新问题的原因)。

更新:在修复版本等之后,最终应用程序运行,但是当我尝试通过 POST 添加用户时,出现如下错误:

https://pastebin.com/raw/d1vqY6xp

Tho*_*win 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 create bean with name 'entityManagerFactory' Invoking of init method failed。您需要确保 hibernate 使用与 @naros 指出的相同版本。