问题
My question can be trival, but documentation for hibernate-envers say I need just 2 steps to make hibernate envers work:
the hibernate-envers jar on the classpath,
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:
generate auto schema using the hbm2ddl tool
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