Hibernate History Quickstart Tutorial
First download the Hibernate History JARs from the repository and include them in the classpath of your project.
Lets start with a example class which is historizable. Objects of the AddressDO class represent address entities which are stored in the database table T_ADDRESSES. There are some sample attributes like name, street, houseNumber, zipcode and city and an id which is also the primary key of the database table.
@Entity @Table(name = "T_ADDRESSES") public class AddressDO implements Historizable { @id @column (name="ADDRESS_ID") private int id; private String name; private String street; private int houseNumber; private int zipCode; private String city; //omitted getter and setter methods }
Here is a sample usage how to initialize the history interceptor and get access to the history entries:
//setting up a sample database BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("org.hsqldb.jdbcDriver"); ds.setUrl("jdbc:hsqldb:mem:hib3DB"); ds.setUsername("sa"); ds.setPassword(""); ds.setMaxActive(2); ds.setDefaultAutoCommit(false); //Setting up a SessionFactory and some properties AutoSessionFactoryBean asfb = new AutoSessionFactoryBean(); asfb.setAnnotatedClasses(new Class[] { AdressDO.class }); asfb.setDataSource(ds); asfb.setSchemaUpdate(true); Properties props = new Properties(); props.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"); props.setProperty("hibernate.cache.use_second_level_cache", "false"); props.setProperty("hibernate.cache.use_query_cache", "false"); asfb.setHibernateProperties(props); //Create a new Interceptor HistoryInterceptor historyInterceptor = new HistoryInterceptor(new HistoryUser()); asfb.setEntityInterceptor(historyInterceptor); changeAutoSessionFactoryBean(asfb); asfb.afterPropertiesSet(); sessionFactory = (SessionFactory) asfb.getObject(); historyInterceptor.setSessionFactory(sessionFactory); hibernate = new HibernateTemplate(sessionFactory); tx = new TransactionTemplate(new HibernateTransactionManager(sessionFactory)); //create a HistoryAdapter final HistoryAdapter ha = new HistoryAdapter(); ha.setSessionFactory(sessionFactory); //create and modify the historizable object tx.execute(new TransactionCallback() { public void doInTransaction(TransactionStatus status) { return hibernate.execute(new HibernateCallback() { public void doInHibernate(Session session) throws HibernateException { HistoryInterceptor.setComment("Creating test address"); AddressDO address = new AddressDO(); address.setName("Max Mustermann"); session.save(address); session.flush(); return; } }); } } //get the history data HistoryEntry[] historyEntries = ha.getHistoryEntries(AddressDO.class);







is the OpenSource platform of 