|
Hibernate example source code file (tutorial_envers.xml)
This example Hibernate source code file (tutorial_envers.xml) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Hibernate tutorial_envers.xml source code
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<chapter id="hibernate-gsg-tutorial-envers">
<title>Tutorial Using Envers
<para>
This tutorial is located within the download bundle under <filename>envers.
</para>
<itemizedlist>
<title>Objectives
<listitem>
<para>
Configure Envers.
</para>
</listitem>
<listitem>
<para>
Use the Envers APIs to view and analyze historical data.
</para>
</listitem>
</itemizedlist>
<section id="hibernate-gsg-tutorial-envers-config">
<title>persistence.xml
<para>
This file was discussed in the <systemitem>JPA tutorial in
<para>
Again, the entity is largely the same as in <xref linkend="hibernate-gsg-tutorial-jpa-entity" />. The major
difference is the addition of the <interfacename>@org.hibernate.envers.Audited annotation, which
tells Envers to automatically track changes to this entity.
</para>
</section>
<section id="hibernate-gsg-tutorial-envers-test">
<title>Example code
<para>
Again, this tutorial makes use of the <systemitem>JPA APIs. However, the code also makes a change to one
of the entities, then uses the Envers API to pull back the initial <firstterm>revision as well as the updated
revision. A revision refers to a version of an entity.
</para>
<example id="hibernate-gsg-tutorial-envers-test-api">
<title>Using the org.hibernate.envers.AuditReader
<programlisting role="JAVA">public void testBasicUsage() {
...
AuditReader reader = AuditReaderFactory.get( entityManager );
Event firstRevision = reader.find( Event.class, 2L, 1 );
...
Event secondRevision = reader.find( Event.class, 2L, 2 );
...
}</programlisting>
</example>
<procedure>
<title>Description of Example
<step>
<para>
An <interfacename>org.hibernate.envers.AuditReader is obtained from the
<classname>org.hibernate.envers.AuditReaderFactory which wraps the
<interfacename>javax.persistence.EntityManager.
</para>
</step>
<step>
<para>
Next, the <methodname>find method retrieves specific revisions of the entity. The first call
reads <literal>find revision number 1 of Event with id 2. The second call reads find
revision number 2 of Event with id 2</literal>.
</para>
</step>
</procedure>
</section>
<section id="hibernate-gsg-tutorial-annotations-further">
<title>Take it further!
<itemizedlist>
<title>Practice Exercises
<listitem>
<para>
Provide a custom revision entity to additionally capture who made the changes.
</para>
</listitem>
<listitem>
<para>
Write a query to retrieve only historical data which meets some criteria. Use the
<citetitle pubwork="book">Envers User Guide to see how Envers queries are constructed.
</para>
</listitem>
<listitem>
<para>
Experiment with auditing entities which have many-to-one, many-to-many relations as well as collections.
Try retrieving historical versions (revisions) of such entities and navigating the object tree.
</para>
</listitem>
</itemizedlist>
</section>
</chapter>
Other Hibernate examples (source code examples)
Here is a short list of links related to this Hibernate tutorial_envers.xml source code file:
|