How to use JHipster and Hibernate Envers

前端 未结 2 1042
臣服心动
臣服心动 2021-02-06 17:31

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

2条回答
  •  离开以前
    2021-02-06 17:56

    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.

      
          org.springframework.data
          spring-data-envers
          0.2.0.RELEASE
      
      
    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 true 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

    
       CREATE DOMAIN "tinyint" AS smallint
    
    

    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

提交回复
热议问题