我有一个 github 存储库,它展示了如何为 jhipster 2.6.0 的 postgres 添加它
一旦你生成了 jhipster 应用程序,你的 postgres 数据库就创建好了,实体也生成了(例如yo jhipster:entity Foo
),并应用所有以前的数据库修订版(运行mvn spring-boot:run
以确保它运行以前的数据库修订版)。
Warning:“spring-data-envers”导致 QueryDsl 中断。 (看:https://github.com/spring-projects/spring-data-envers/issues/30 https://github.com/spring-projects/spring-data-envers/issues/30)。此外,https://github.com/spring-projects/spring-data-envers/issues/33#issuecomment-108796022 https://github.com/spring-projects/spring-data-envers/issues/33#issuecomment-108796022说“spring-data-envers”项目不是优先事项。 Jhipster 已经包含的“hibernate-envers”项目允许您使用 envers,而不需要“spring-data-envers”...因此,如果您想避免 QueryDsl 问题,请跳过步骤 1 和 3 来删除“spring-data-envers”。
-
将 spring-data-envers 添加到您的 pom.xml 中。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>0.2.0.RELEASE</version>
</dependency>
Add @Audited
到域包中的实体类
- Add
repositoryFactoryBeanClass=org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean.class
到 DatabaseConfiguration.java 中的 @EnableJpaRepositories 注释(即更改该行,使其类似于@EnableJpaRepositories(basePackages="com.mycompany.myapp.repository", repositoryFactoryBeanClass=EnversRevisionRepositoryFactoryBean.class
)
- 添加扩展 DefaultRevisionEntity 的 CustomRevisionEntity 以将任何特殊内容添加到修订表中(例如,进行更改的登录名)
- 添加实现 RevisionListener 的 CustomRevisionListener 以设置 CustomRevisionEntity 中的字段
-
mvn liquibase:diff
将生成您的变更日志以添加审核
- 将变更日志添加到 src/main/resources/config/liquibase/master.xml
Note: liquibase-3.3.2 无法识别 INT4 for postgres 的 autoIncrement 并会抛出此错误java.lang.RuntimeException: Unknown property autoIncrement for liquibase.datatype.core.UnknownType INT4
。您可以将其更改为“SERIAL”,但您将与进一步生成的变更日志作斗争。我建议升级到 liquibase-3.3.3 (请参阅https://github.com/liquibase/liquibase/commit/1602ddf1cf4968753e09a6858fc1580230a2fb44 https://github.com/liquibase/liquibase/commit/1602ddf1cf4968753e09a6858fc1580230a2fb44)但你还必须添加<liquibaseShouldRun>true</liquibaseShouldRun>
到 pom.xml 中的 liquibase 插件配置。
Note:当您运行测试(使用 H2)时,H2 期望 REVTYPE 为tinyint 而不是smallint。要么忽略测试,要么添加类似的内容
<changeSet author="sdoxsee" id="1426529918864-0">
<sql dbms="postgresql">CREATE DOMAIN "tinyint" AS smallint</sql>
</changeSet>
这样 H2 就会开心,postgres 也会开心。否则你会得到
org.hibernate.HibernateException: Wrong column type in JHIPSTER.PUBLIC.T_FOO_AUD for column REVTYPE. Found: smallint, expected: tinyint
当你跑步时mvn test
-
mvn spring-boot:run
再次应用变更日志
- 通过正在运行的应用程序创建一个实体
- 打开pgAdmin3可以查看审计表历史记录!
希望这可以帮助。
我已经包含了来源:https://github.com/sdoxsee/jhipster-app-envers https://github.com/sdoxsee/jhipster-app-envers
有用的参考:
- http://hantsy.blogspot.ca/2013/11/auditing-with-hibernate-envers.html http://hantsy.blogspot.ca/2013/11/auditing-with-hibernate-envers.html