Abhishek Somani

About Abhishek Somani

Abhishek is working as a senior java developer in a product start up .He has worked on various java related enterprise applications and frameworks. He loves to explore new technologies

How to maintain history of tables in Hibernate

To Maintain history of database or keep track of modification of database table row , we create a version table that contains fields identical to original table.Whenever original table gets changed , we create another entry in version table. So for every update query , we have to write an insert query in version table . There is a module available in hibernate to manage easy auditing of objects , and we don’t have to write separate insert query by ourselves.

Hibernate Envers provides in built mechanism to maintain history of objects in database . Envers is library for Hibernate that will help us to easily achieve audit functionality. This has been created by Adam Warski. From Hibernate 3.5, Envers is included as a Hibernate core module. Let’s take an example as to how Envers can be used to maintain history of objects.

Here is the pom dependency for Envers (Version will be same as your hibernate core and entity manager):

<dependency>  <groupId>org.hibernate</groupId> 
  <artifactId>hibernate-envers</artifactId> 
  <version>4.0.1.Final</version> 
</dependency>

You have to configure listeners in your hibernate.cfg.xml.

<mapping class="com.javaroots.model.User" />

  <listener class="org.hibernate.envers.event.AuditEventListener" type="post-insert"/>
  <listener class="org.hibernate.envers.event.AuditEventListener" type="post-update"/>
  <listener class="org.hibernate.envers.event.AuditEventListener" type="post-delete"/>
  <listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-update"/>
  <listener class="org.hibernate.envers.event.AuditEventListener" type="pre-collection-remove"/>
  <listener class="org.hibernate.envers.event.AuditEventListener" type="post-collection-recreate"/>

Let’s take an example of User class . We want to keep track of user fields updating . To Enable history for User Object , we need to use @Audited annotation . If it used on class level , all the fields in the class will be treated as audit-able and change in any of the fields will have a new entry in audit table . If we want some fields not to be included in history , we can use @NotAudited annotation . If a NotAudited field is changed , there will be no entry in audit table.Here is the user class :

package com.javaroots.model;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Entity;

import org.hibernate.envers.Audited;
import org.hibernate.envers.NotAudited;

/**
 * 
 * 
 * @author Abhishek Somani
 * 
 */
@Entity
@Audited
public class User {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;
 @Column(length = 20)
 private String firstName;
 @Column(length = 20)
 private String lastName;
 @Column(length = 20)
 @NotAudited
 private String password;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}

Here is the test class , where we are creating an entry in user table and then updating it’s field .

package com.javaroots.main;

import org.hibernate.Session;

import com.javaroots.model.User;
import com.javaroots.util.HibernateUtil;

public class HibernateTest {

public static void main(String[] args) {

        Session session = HibernateUtil.getSessionFactory().openSession();

        //one entry will be created in user table 
        //and audit entry created in user_aud table
        session.beginTransaction();

        User u = new User();
        u.setFirstName("Amitabh");
        u.setLastName("bachhan");
        u.setPassword("God");

        session.save(u);

        session.getTransaction().commit();

        session.beginTransaction();
        User amitabh = (User)session.get(User.class,1l);

        amitabh.setFirstName("Abhishek");

        session.getTransaction().commit();

        //no entry in audit table if we change password field
        //because this field is marked as @notAudited
        session.beginTransaction();
        amitabh = (User)session.get(User.class,1l);

        amitabh.setPassword("NotGod");

        session.getTransaction().commit();

   //get specific revision
        AuditReader reader = AuditReaderFactory.get(HibernateUtil.getSessionFactory().openSession());
        User abhishek = (User) reader.find(User.class, new Long(1), 2);
        System.out.println(abhishek.getFirstName() + " " + abhishek.getLastName());

        //get all revision

        List versions = reader.getRevisions(User.class, new Long(1));
        for (Number number : versions) {
          System.out.print(number + " ");
        }

    }

}

First ,a user row is created in user table .one row is created in user_aud with revision id , and user table fields . One row is created in revinfo table with revision id and timestamp . These two entries are done by envers automatically . Here are the sql queries and table structures :

Hibernate: 
    insert 
    into
        User
        (firstName, lastName, password) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        REVINFO
        (REVTSTMP) 
    values
        (?)
Hibernate: 
    insert 
    into
        User_AUD
        (REVTYPE, firstName, lastName, id, REV) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    update
        User 
    set
        firstName=?,
        lastName=?,
        password=? 
    where
        id=?
Hibernate: 
    insert 
    into
        REVINFO
        (REVTSTMP) 
    values
        (?)
Hibernate: 
    insert 
    into
        User_AUD
        (REVTYPE, firstName, lastName, id, REV) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    update
        User 
    set
        firstName=?,
        lastName=?,
        password=? 
    where
        id=?
Hibernate: 
    select
        user_aud0_.id as id4_,
        user_aud0_.REV as REV4_,
        user_aud0_.REVTYPE as REVTYPE4_,
        user_aud0_.firstName as firstName4_,
        user_aud0_.lastName as lastName4_ 
    from
        User_AUD user_aud0_ 
    where
        user_aud0_.REV=(
            select
                max(user_aud1_.REV) 
            from
                User_AUD user_aud1_ 
            where
                user_aud1_.REV<=? 
                and user_aud0_.id=user_aud1_.id
        ) 
        and user_aud0_.REVTYPE<>? 
        and user_aud0_.id=?
Abhishek bachhan
Hibernate: 
    select
        user_aud0_.REV as col_0_0_ 
    from
        User_AUD user_aud0_ cross 
    join
        REVINFO defaultrev1_ 
    where
        user_aud0_.id=? 
        and user_aud0_.REV=defaultrev1_.REV 
    order by
        user_aud0_.REV asc
mysql> select * from user;
+----+-----------+----------+---------------+
| id | firstName | lastName | password      |
+----+-----------+----------+---------------+
|  1 | Amitabh   | bachchan    | God|
+----+-----------+----------+---------------+
1 row in set (0.03 sec)

mysql> select * from user_aud;
+----+-----+---------+-----------+----------+
| id | REV | REVTYPE | firstName | lastName |
+----+-----+---------+-----------+----------+
|  1 |   1 |       0 | Amitabh   | bachchan    |
+----+-----+---------+-----------+----------+
1 row in set (0.00 sec)

mysql> select * from revinfo;
+-----+---------------+
| REV | REVTSTMP      |
+-----+---------------+
|   1 | 1375956506278|
+-----+---------------+
1 row in set (0.00 sec)
mysql> select * from user;
+----+-----------+----------+----------------+
| id | firstName | lastName | password       |
+----+-----------+----------+----------------+
|  1 | Amitabh   | bachchan| NotGod |
+----+-----------+----------+----------------+
1 row in set (0.00 sec)

mysql> select * from user_aud;
+----+-----+---------+-----------+----------+
| id | REV | REVTYPE | firstName | lastName |
+----+-----+---------+-----------+----------+
|  1 |   1 |       0 | Amitabh   | bachchan |
|  1 |   2 |       1 | Abhishek  | bachchan|
+----+-----+---------+-----------+----------+
2 rows in set (0.00 sec)

mysql> select * from revinfo;
+-----+---------------+
| REV | REVTSTMP      |
+-----+---------------+
|   1 | 1375956506278|
|   2 | 1375956506328|
+-----+---------------+
2 rows in set (0.00 sec)

 

Reference: How to maintain history of tables in Hibernate from our JCG partner Abhishek Somani at the Java, J2EE, Server blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

2 Responses to "How to maintain history of tables in Hibernate"

  1. jordi says:

    Could you please give an example with relations? 1-n and N-M thats a bit more tricky isn’t it
    Thanks

Leave a Reply


7 − two =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books