带有liquibase的hibernate-envers
我的问题可能很简单,但是有关hibernate-envers的文档说,我只需要2个步骤就可以使hibernate-envers起作用:
My question can be trival, but documentation for hibernate-envers say I need just 2 steps to make hibernate envers work:
-
类路径上的hibernate-envers jar
the hibernate-envers jar on the classpath,
有关实体的@Audited注释.
an @Audited annotation on the entity.
我首先添加:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.2.12.Final</version>
</dependency>
第二,我在实体上方添加了@Audited注释:
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:
-
使用hbm2ddl工具生成自动模式
generate auto schema using the hbm2ddl tool
单独创建架构.
教程/文档等中的许多ppl都说您应该单独进行操作,因此这就是我的架构(在百里香中)的样子.
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).
要手动创建模式,我必须使用一个特殊表(通常称为revinfo)创建变更集:
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>
现在,我只需要添加名为tablename_AUD的新表(可以通过在我的实体@AuditTable("new_name")中添加新注释来更改此名称,但情况并非如此),并添加了rev和revtype列.
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.
这是我在liquibase中现有的表之一:
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>
这就是我的users_AUD的样子:
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:错误创建在类路径中定义的名称为'entityManagerFactory'的bean资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]:调用init方法失败;嵌套的例外是java.lang.NoClassDefFoundError:org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl $ Work
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中也添加了实体管理人员的权限,但这并不能解决我的问题.
I added also entity manager depedency in my pom, but it didnt solve my problem.
先前称为:
这种映射是可能的,但是必须使用来明确定义@Audited(targetAuditMode = NOT_AUDITED)
Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED)
@ThomasEdwin的回答帮助了我.他说我们需要
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).
许多类似的问题已经过时了(之前配置/设置Envers的需求更多,这就是为什么我创建了新问题).
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).
更新:在修复了版本等之后,最终应用程序运行了,但是当我尝试通过POST添加用户时,出现了如下错误:
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:
这种映射是可能的,但是必须使用@Audited(targetAuditMode = NOT_AUDITED)明确定义
Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED)
您需要将 @Audited
添加到所有相关实体.
You need to add @Audited
to all related entities.
org.springframework.beans.factory.BeanCreationException:错误创建在类路径中定义的名称为'entityManagerFactory'的bean资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]:调用init方法失败;嵌套的例外是java.lang.NoClassDefFoundError:org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl $ Work
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
这可能会有所帮助:创建bean时出错名称为'entityManagerFactory'的init方法调用失败.您需要确保休眠使用与@naros指出的版本相同的版本.
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.