How to use JHipster and Hibernate Envers

我与影子孤独终老i 提交于 2019-12-21 06:09:30

问题


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.

  1. 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>
    
  2. Add @Audited to your entity class in the domain package

  3. 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)
  4. Add CustomRevisionEntity that extends DefaultRevisionEntity to add anything special to your revision tables (e.g. the login of who made the change)
  5. Add CustomRevisionListener that implements RevisionListener to set the fields in your CustomRevisionEntity
  6. mvn liquibase:diff will generate your changelog for adding auditing
  7. 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

  1. mvn spring-boot:run again will apply the changelog
  2. Create an entity through the running app
  3. 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

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