带 liquibase 的 hibernate-envers

the hibernate-envers with liquibase

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

  1. classpath 上的 hibernate-envers jar,

  2. 实体上的@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 {...}

完成这两个步骤后,我可以选择:

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

  2. 单独创建架构。

tutorials/docs 等很多人说你应该自己做,所以这就是我的模式(在 thymeleaf 中)的样子。

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

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

现在我只需要添加名为 tablename_AUD 的新 table(当然我可以通过在实体 @AuditTable("new_name") 中添加新注释来更改此名称),但它不是这种情况)添加了 rev 和 revtype 列。

这是我在 liquibase 中现有的 table 之一:

    <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: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work

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

之前的调用:

Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED)

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

add @Audited to all related entities.

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

@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED).

很多类似的问题都已经过时了(之前 configure/set 需要更多的问题,这就是我创建新问题的原因)。

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

https://pastebin.com/raw/d1vqY6xp

Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED)

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

编辑:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work

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