2 * Hibernate, Relational Persistence for Idiomatic Java
4 * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Inc.
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
24 package org.hibernate.tutorial.envers;
26 import java.util.Date;
27 import java.util.List;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.Persistence;
32 import junit.framework.TestCase;
34 import org.hibernate.envers.AuditReader;
35 import org.hibernate.envers.AuditReaderFactory;
38 * Illustrates the set up and use of Envers.
40 * This example is different from the others in that we really need to save multiple revisions to the entity in
41 * order to get a good look at Envers in action.
43 * @author Steve Ebersole
45 public class EnversIllustrationTest extends TestCase {
46 private EntityManagerFactory entityManagerFactory;
49 protected void setUp() throws Exception {
50 // like discussed with regards to SessionFactory, an EntityManagerFactory is set up once for an application
51 // IMPORTANT: notice how the name here matches the name we gave the persistence-unit in persistence.xml!
52 entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.envers" );
56 protected void tearDown() throws Exception {
57 entityManagerFactory.close();
60 public void testBasicUsage() {
61 // create a couple of events
62 EntityManager entityManager = entityManagerFactory.createEntityManager();
63 entityManager.getTransaction().begin();
64 entityManager.persist( new Event( "Our very first event!", new Date() ) );
65 entityManager.persist( new Event( "A follow up event", new Date() ) );
66 entityManager.getTransaction().commit();
67 entityManager.close();
69 // now lets pull events from the database and list them
70 entityManager = entityManagerFactory.createEntityManager();
71 entityManager.getTransaction().begin();
72 List<Event> result = entityManager.createQuery( "from Event", Event.class ).getResultList();
73 for ( Event event : result ) {
74 System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
76 entityManager.getTransaction().commit();
77 entityManager.close();
79 // so far the code is the same as we have seen in previous tutorials. Now lets leverage Envers...
81 // first lets create some revisions
82 entityManager = entityManagerFactory.createEntityManager();
83 entityManager.getTransaction().begin();
84 Event myEvent = entityManager.find( Event.class, 2L ); // we are using the increment generator, so we know 2 is a valid id
85 myEvent.setDate( new Date() );
86 myEvent.setTitle( myEvent.getTitle() + " (rescheduled)" );
87 entityManager.getTransaction().commit();
88 entityManager.close();
90 // and then use an AuditReader to look back through history
91 entityManager = entityManagerFactory.createEntityManager();
92 entityManager.getTransaction().begin();
93 myEvent = entityManager.find( Event.class, 2L );
94 assertEquals( "A follow up event (rescheduled)", myEvent.getTitle() );
95 AuditReader reader = AuditReaderFactory.get( entityManager );
96 Event firstRevision = reader.find( Event.class, 2L, 1 );
97 assertFalse( firstRevision.getTitle().equals( myEvent.getTitle() ) );
98 assertFalse( firstRevision.getDate().equals( myEvent.getDate() ) );
99 Event secondRevision = reader.find( Event.class, 2L, 2 );
100 assertTrue( secondRevision.getTitle().equals( myEvent.getTitle() ) );
101 assertTrue( secondRevision.getDate().equals( myEvent.getDate() ) );
102 entityManager.getTransaction().commit();
103 entityManager.close();