Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audit table using "Envers" in Spring Hibernate java project

We need to audit the existing table using envers. we don't have hibernate.xml instead of we are using application-context.xml. And we are creating schema through "liquibase-changeset", then how do I create through annotations like @Entity and @Audited.

How do I solve this issue?

I have added hibernate configuration likes

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.ejb.event.post-insert">org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.post-update">org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.post-delete">org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.pre-collection-update">org.hibernate.envers.event.AuditEventListener</prop>
                <!-- <prop key="hibernate.ejb.event.pre-collection-remove">org.hibernate.envers.event.AuditEventListener</prop>
                <prop key="hibernate.ejb.event.post-collection-recreate">org.hibernate.envers.event.AuditEventListener</prop> -->
                <prop key="org.hibernate.envers.revision_field_name">REV</prop>
                <prop key="org.hibernate.envers.revision_type_field_name">REVTYPE</prop>
                <prop key="org.hibernate.envers.auditTablePrefix"></prop>
                <prop key="org.hibernate.envers.auditTableSuffix">_HISTORY</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>

Added @Audited annotation in my domain class

@Entity
@Audited
@Table(name="user")
public class User implements Serializable {

But this configuration deleted my existing tables

e.g

Mydatabase
-----------

user
product
order_details
user_role
login

I have 5 tables in my database. After running my application it displays 3 tables. Instead of creating "audit" table, it deletes the existing table.

 Mydatabase
  -----------

  user
  product
  order_details

How to create audit(_HISTORY) table without touching existing tables???

like image 748
SST Avatar asked Jan 16 '15 09:01

SST


Video Answer


1 Answers

In the Liquibase changeset define the audit table definition like you would for any other table.

Skip the hibernate.hbm2ddl.auto property in spring-hibernate ocnfiguration.That will instruct hibernate to not do anything to the schema.

Keeping rest of your configuartion as it is, this should work.

Just ensure the audit tables names in schema and in configuration match.

Link to doc detailing how its done in case schema is generated using ant

like image 66
Rohit Avatar answered Oct 14 '22 20:10

Rohit