the hibernate-envers with liquibase

[亡魂溺海] 提交于 2021-02-07 19:22:35

问题


My question can be trival, but documentation for hibernate-envers say I need just 2 steps to make hibernate envers work:

  1. the hibernate-envers jar on the classpath,

  2. an @Audited annotation on the entity.

First of all I added:

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

Second I added @Audited annotation above my entity:

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

After those 2 steps I've got an option to:

  1. generate auto schema using the hbm2ddl tool

  2. create the schema alone.

A lot of ppl in tutorials/docs etc. says that you should do it alone so this is how my schema looks like (in thymeleaf).

To make schema manually I had to create changeset with one special table (usually called revinfo):

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

Now I just need to add new tables called tablename_AUD (ofc I can change this name by adding new annotation in my entity @AuditTable("new_name"), but it's not the case) with added rev and revtype column.

This is one of my existing tables in 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>

So this is how my users_AUD looks like:

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

The question is: Why I have an ERROR:

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

I added also entity manager depedency in my pom, but it didnt solve my problem.

Earlier called:

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

And answer from @ThomasEdwin helped me out. He said that we need to

add @Audited to all related entities.

Or like I noticed we can also annotate in our entity

@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED).

A lot of similar questions are out-of-date (earlier there was much more needed to configure/set up envers, thats why I created new question).

update: After fixes with versions etc. finally app runs, but when I try to add an user by a POST I've got error like this:

https://pastebin.com/raw/d1vqY6xp


回答1:


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

You need to add @Audited to all related entities.

Edit:

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

This may help: Error creating bean with name 'entityManagerFactory' Invocation of init method failed. You need to make sure hibernate use the same version as pointed out by @naros.



来源:https://stackoverflow.com/questions/47810697/the-hibernate-envers-with-liquibase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!