Hibernate envers custom revinfo table

In this tutorial we will teach you use the custom revinfo table in Hibernate envers.

Hibernate envers custom revinfo table

Hibernate envers custom revinfo table - How to specify custom  revinfo table?

In this tutorial we are going to teach you to configure the Hibernate enver application to use the custom revinfo table. In your project if you don't want to use the default revinfo table then you should make changes given in the tutorial.

In Hibernate envers its possible to use the custom revinfo table in your application. The Hibernate envers is used to manage the versions of the data which is updated in the database over the time. This library is very easy to use and flexible, developers use this library to easily develop such projects.

In the previous sessions you learn following things:

How to use custom revinfo table?

We will see the steps for enabling the custom revinfo table.

Step 1: Create CustomRevisionListener class

First of all create a CustomRevisionListener class in your project. Here is the full code:

package net.roseindia.model;

import org.hibernate.envers.RevisionListener;

public class CustomRevisionListener implements RevisionListener {

@Override
public void newRevision(Object revisionEntity) {
}
}

 

Step 2: Create CustomRevisionEntity class and specify the custom revinfo table name

The next step is to create the CustomRevisionEntity class which takes the name of custom revinfo table name. In our case the custom revinfo table is "TBL_REVINFO".

Here is the full code of the class:

package net.roseindia.model;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.hibernate.envers.DefaultRevisionEntity;
import org.hibernate.envers.RevisionEntity;



@Entity
@RevisionEntity(CustomRevisionListener.class)
@Table( name = "TBL_REVINFO")
public class CustomRevisionEntity extends DefaultRevisionEntity{

private static final long serialVersionUID = 1L;
}

 

Step 3: Now create your entity class

Now you can create your entity class. Here is full code from our example project:

package net.roseindia.model;
import java.io.Serializable;

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

import org.hibernate.envers.Audited;
/**
 * @author Deepak Kumar
 * Web: http://www.roseindia.net
 */
@Entity
@Audited
@Table(name = "article")
public class Article implements Serializable{

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY) 
	@Column(name="id")
	private int id;	

	@Column(name="title")
	private String title;

	@Column(name="content")
	private String content;

	public int getId() {
		return id;
	}

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

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}	  

	
}

Here is the structure of article table:

CREATE TABLE `article` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(45) DEFAULT NULL,
  `content` varchar(400) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

Step 4: Enable hibernate.hbm2ddl.auto value in hibernate.cfg.xml

Now in the hibernate.cfg.xml file you should use following code:

<property name="hibernate.hbm2ddl.auto">update</property>

Apart from this also add the CustomRevisionEntity class as one of the entity class as shown below:

<mapping class="net.roseindia.model.CustomRevisionEntity" />

Here is the full code of the hibernate.cfg.xml file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate5envers</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<mapping class="net.roseindia.model.Article" />
<mapping class="net.roseindia.model.CustomRevisionEntity" />


</session-factory>
</hibernate-configuration>

Project also have following classes which you will get in the downloaded source code:

HibernateUtil.java: It generates the SessionFactory for the application.

Sep 5: Run the application

You can test the application by running the CreateData.java file. Full source code of CreateData.java:

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 CreateData {
	public static void main(String[] args) throws Exception {

		SessionFactory sessFact = HibernateUtil.getSessionFactory();
		Session session = sessFact.getCurrentSession();
		org.hibernate.Transaction tr = session.beginTransaction();
		Article article = new Article();
		article.setTitle("Java Tutorial");
		article.setContent("Read all tutorials at http://www.roseindia.net");

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

}

Step 6: Check database table

Now you can check you database and there should be one table "TBL_REVINFO" which stores the revision details in the database.

The structure of "TBL_REVINFO" is as follows: 0

CREATE TABLE `tbl_revinfo` (
  `id` int(11) NOT NULL,
  `timestamp` bigint(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In this tutorial you learned how to use the custom revinfo table in Hibernate envers.

Download Source code of application

  1