How to save history of each record using Spring Roo gvnix Audit

269 views Asked by At

I would like to save my My Employee details in Employee table and History details EmployeeHistory table before running the below command i am able to save the records in my Employee table but not saving in EmployeeHistory table jpa audit revisionLog --provider H-ENVERS

After running the above command i am not able to save Employee table also i am getting error like this org.hibernate.engine.jdbc.spi.sqlexceptionhelper - invalid object name RevisionLogEntity

My log.roo

project --topLevelPackage com.employeedetails --projectName EmployeeDetails
jpa setup --database MSSQL --provider HIBERNATE --hostName Hostname --databaseName dbname --userName sa --password Admin@123!!
database introspect --schema dbo --file db_schema
database reverse engineer --schema dbo --package ~.domain --includeTables "Employee"
web mvc setup
web mvc all --package ~.web
web mvc scaffold --class ~.web.EmployeeController --backingType ~.domain.Employee
web mvc scaffold --class ~.web.EmployeeListController --backingType ~.domain.Employee --path employeelist

web mvc finder all
web mvc jquery setup
web mvc jquery all
web mvc datatables setup
web mvc bootstrap setup
jpa gvnix setup
jpa batch all
web mvc batch setup
web mvc batch all
web mvc datatables add --type ~.web.EmployeeListController --mode show
security setup
web mvc bootstrap update
jpa audit setup
jpa audit revisionLog --provider H-ENVERS
jpa audit all --package ~.domain.audit

enter image description here

1

There are 1 answers

0
jmvivo On BEST ANSWER

I can see that you are using reverse engineering to generate your model. Did you create the tables required by Hibernate Envers to store revision information?

Hibernate Envers requires a new entity RevisionLog to store general information about all revision (date, user, etc) which is stored in DB and one table per entity audited to store entity instance values for every revision.

gvNIX create this configuration for you but, as you are using Reverse Engineering tool, the hibernate.hbm2ddl.auto property should be set to none. So, required Envers tables will not be generated on DB.

A simple solution could be:

  1. Create a temporal empty DB
  2. Configure your project to use it
  3. Set hibernate.hbm2ddl.auto to create
  4. Start the application: Hibernate hbm2ddl will generate required tables
  5. Stop application
  6. Create all envers tables in original DB based on its definition of temporal DB
  7. Restore DB and hbm2ddl settings in your project

For more information about Envers check development guide

Good luck!

P.D.: Don't forget exclude envers tables from Reverse Engineering configuration next time you execute it!.