Hibernate Envers get all revisions

In this section we will teach you to use Hibernate Envers to get all the revisions of an entity.

Hibernate Envers get all revisions

Hibernate Envers get all revisions - Example of getting all the revision of an entity

In the previous example we showed you how to use the Hibernate Evers module to audit and entity. In this section we will further enhance this program and show to query and view the entity audits.


We will make program to update entity and then show you how to get all the revisions of an entity from the audit log programmatically using the Hibernate Envers API.


Auditing Entity Example

Now we will write simple program that uses Hibernate Session to load the entity which we created in last session and updates the entity data. Finally entity is saved using Hibernate. This process creates two versions in the database. Here is the full code of the program:

package net.roseindia;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import net.roseindia.model.*;
/**
 * @author Deepak Kumar 
 * Web: http://www.roseindia.net
 */
public class UpdateData {
	public static void main(String[] args) throws Exception {

		SessionFactory sessFact = HibernateUtil.getSessionFactory();
		Session session = sessFact.getCurrentSession();
		org.hibernate.Transaction tr = session.beginTransaction();
		
		Article article =  (Article)session.get(Article.class, new Integer(2));
		
		article.setTitle("Best Java Tutorial");
		article.setContent("Visit at http://www.roseindia.net");

		session.update(article);
		tr.commit();
		System.out.println("Successfully inserted");
		sessFact.close();
	}

}

Note: You you check you database and change the value of primary key in the line:

Article article =  (Article)session.get(Article.class, new Integer(2));

of above code.

Now run the program in Eclipse to update data and this process will generate the entity audit data.

Output of the program:

Apr 19, 2017 9:03:37 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.8.Final}
Apr 19, 2017 9:03:37 AM org.hibernate.cfg.Environment 
INFO: HHH000206: hibernate.properties not found
Apr 19, 2017 9:03:37 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration.
  Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  
    Support for obsolete DTD/XSD namespaces may be removed at any time.
Apr 19, 2017 9:03:38 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager 
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Apr 19, 2017 9:03:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Apr 19, 2017 9:03:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate5]
Apr 19, 2017 9:03:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Apr 19, 2017 9:03:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Apr 19, 2017 9:03:38 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections 
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
Apr 19, 2017 9:03:38 AM org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Apr 19, 2017 9:03:38 AM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Apr 19, 2017 9:03:38 AM org.hibernate.envers.boot.internal.EnversServiceImpl configure
INFO: Envers integration enabled? : true
Hibernate: select article0_.id as id1_0_0_, article0_.content as content2_0_0_, article0_.title as title3_0_0_ from article article0_ 
  where article0_.id=?
Hibernate: update article set content=?, title=? where id=?
Hibernate: insert into REVINFO (REVTSTMP) values (?)
Hibernate: insert into article_AUD (REVTYPE, content, title, id, REV) values (?, ?, ?, ?, ?)
Successfully inserted
Apr 19, 2017 9:03:40 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate5]

Here is the screen shot of update program in the Eclipse IDE:

Getting all revision of Entity

Lets write program to get of revisions of entity using Hibernate Envers API.

In Hiberntae we will use the AuditReader class of Hibernate Envers API which will reads the versions of the data and return as Article Object.

Here is complete example:

package net.roseindia;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;

import javax.persistence.criteria.CriteriaQuery;
import net.roseindia.model.*;

/**
 * @author Deepak Kumar Web: http://www.roseindia.net
 */
public class GetAllRevisions {
	public static void main(String[] args) throws Exception {

		SessionFactory sessFact = HibernateUtil.getSessionFactory();
		Session session = sessFact.getCurrentSession();
		
		org.hibernate.Transaction tr = session.beginTransaction();
		
		AuditReader reader = AuditReaderFactory.get(session);
		List revisions = reader.getRevisions(Article.class, 2);
		for(Number revNum:revisions){
			Article article =reader.find(Article.class, 2, revNum);
			System.out.println("Revision No: " + revNum);
			System.out.println("Title: " + article.getTitle());
			System.out.println("Content: " + article.getContent());		

		}
		
	
		tr.commit();
		
		System.out.println("Data printed");
		sessFact.close();
	}
}

In this tutorial you learned how to query the transaction/version data from the database. You can download the code below:

Download source code of Hibernate 5 Envers Example.