问题
I am having trouble figuring out how to use Hibernate Envers and JHipster.
I am using PostgreSQL to store the data, and latest Jhipster 2.6.0 I just generated a JHipster application, with no modifications at all. The User domain class extends the AbstractAuditingEntity class, which has the @Audited annotation, but when editing a user there's no t_user_aud table created in the database.
Is there any configuration needed in order to have Hibernate Envers saving modifications ?
回答1:
I've got a github repository that shows how to add it for postgres for jhipster 2.6.0
Once you have your jhipster app generated, your postgres db created, entity generated (e.g. yo jhipster:entity Foo
), and all previous db revision applied (run mvn spring-boot:run
to make sure it runs previous db revisions).
Warning: 'spring-data-envers' causes breaks QueryDsl. (See: https://github.com/spring-projects/spring-data-envers/issues/30). Furthermore, https://github.com/spring-projects/spring-data-envers/issues/33#issuecomment-108796022 says that the 'spring-data-envers' project is not a priority. Jhipster's already-included 'hibernate-envers' project lets you use envers without needing 'spring-data-envers'...so if you want to avoid QueryDsl problems, remove 'spring-data-envers' by skipping step 1 and 3.
Add spring-data-envers to your pom.xml.
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-envers</artifactId> <version>0.2.0.RELEASE</version> </dependency>
Add
@Audited
to your entity class in the domain package- Add
repositoryFactoryBeanClass=org.springframework.data.envers.repository.support.EnversRevisionRepositoryFactoryBean.class
to your @EnableJpaRepositories annotation in DatabaseConfiguration.java (i.e. change the line so that it is something like@EnableJpaRepositories(basePackages="com.mycompany.myapp.repository", repositoryFactoryBeanClass=EnversRevisionRepositoryFactoryBean.class
) - Add CustomRevisionEntity that extends DefaultRevisionEntity to add anything special to your revision tables (e.g. the login of who made the change)
- Add CustomRevisionListener that implements RevisionListener to set the fields in your CustomRevisionEntity
mvn liquibase:diff
will generate your changelog for adding auditing- Add the changelog to src/main/resources/config/liquibase/master.xml
Note: liquibase-3.3.2 doesn't recognize INT4 for autoIncrement for postgres and would throw this
java.lang.RuntimeException: Unknown property autoIncrement for liquibase.datatype.core.UnknownType INT4
. You can change it to 'SERIAL' instead but you'll fight with further generated changelogs. I'd recommend upgrading to liquibase-3.3.3 (see https://github.com/liquibase/liquibase/commit/1602ddf1cf4968753e09a6858fc1580230a2fb44) but you'll also have to add <liquibaseShouldRun>true</liquibaseShouldRun>
to the liquibase plugin configuration in your pom.xml.
Note: When you run your tests (using H2), H2 expects tinyint instead of smallint for the REVTYPE. Either ignore the tests or add something like this
<changeSet author="sdoxsee" id="1426529918864-0">
<sql dbms="postgresql">CREATE DOMAIN "tinyint" AS smallint</sql>
</changeSet>
so that H2 will be happy and postgres will be happy. Otherwise you'll get
org.hibernate.HibernateException: Wrong column type in JHIPSTER.PUBLIC.T_FOO_AUD for column REVTYPE. Found: smallint, expected: tinyint
when you run mvn test
mvn spring-boot:run
again will apply the changelog- Create an entity through the running app
- Open pgAdmin3 to see the audit table history!
Hope this helps.
I've included the source: https://github.com/sdoxsee/jhipster-app-envers
A helpful reference:
- http://hantsy.blogspot.ca/2013/11/auditing-with-hibernate-envers.html
回答2:
We currently use the PersistentAuditEvent entity to save changes, and not the "normal" t_user_aud table.
This is because we add additional information (this is for security audits), compared to what Envers stores by default.
来源:https://stackoverflow.com/questions/29290519/how-to-use-jhipster-and-hibernate-envers