In short, Enver is version control, logging database operations, controlling granularity as a field, similar in principle to SVN
1.pom.xml configuration and Event.java definition reference http://my.oschina.net/u/555061/blog/506052
2. Directory structure is different
Enver is configured with the map XML, The original mapping is configured as Hibernate.cfg.xml, now Persistence.xml, and the Persistence.xml directory is the next level of Meta-inf (directory issues should be configurable, but I have not studied for the time being)
3. View the next persistence.xml
<persistence xmlns= "Http://java.sun.com/xml/ns/persistence" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://java.sun.com/xml/ns/ Persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd " version= "2.0" > <persistence-unit name= "Org.hibernate.tutorial.envers" > <description > persistence unit for the Envers tutorial of the Hibernate Getting Started Guide </description> <class>org.hibernate.tutorial.envers.event</class> <properties> <property name= " Javax.persistence.jdbc.driver " value=" Com.mysql.jdbc.Driver " /> <property name= "Javax.persistence.jdbc.url" value= "jdbc : Mysql://192.168.191.1:3306/mysql " /> <property name= "Javax.persistence.jdbc.user" value= "root" /> <property name= " Javax.persistence.jdbc.password " value=" root " /> <property name= "Hibernate.show_sql" value= "true" /> <property&nbsP;name= "Hibernate.hbm2ddl.auto" value= "create" /> </properties> </persistence-unit></persistence>
Change is not too big, mainly
<mapping class= "Org.hibernate.tutorial.annotations.Event"/>
Changed to
<class>org.hibernate.tutorial.envers.Event</class>
In fact, that is the wording of the rules.
4. Configuration issues are no longer a major issue, and version control viewing applications are major issues, as follows
package org.hibernate.tutorial.envers;import java.text.simpledateformat;import java.util.date; import java.util.list;import java.util.logging.logger;import javax.persistence.entitymanager; import javax.persistence.entitymanagerfactory;import javax.persistence.persistence;import junit.framework.testcase;import org.hibernate.envers.auditreader;import org.hibernate.envers.auditreaderfactory;public class enversillustrationtest extends Testcase {private logger logger = logger.getlogger ( EnversIllustrationTest.class.getName ());p rivate entitymanagerfactory entitymanagerfactory;@ Overrideprotected void setup () throws Exception {entityManagerFactory = Persistence.createentitymanagerfactory ("Org.hibernate.tutorial.envers");} @Overrideprotected void teardown () throws exception {entitymanagerfactory.close ();} Public void testbAsicusage () {// Here is the main demonstration of how to use Entitymanagerfactory to operate the database, inserting 2 data Entitymanager entitymanager = entitymanagerfactory.createentitymanager (); Entitymanager.gettransaction (). Begin (); Entitymanager.persist (New event ("our very first event!", new date ())); Entitymanager.persist (New event ("A follow up event", new date ())); Entitymanager.gettransaction (). commit (); Entitymanager.close ();// Here is how to use Entitymanagerfactory to query out objects from a database Listentitymanager = entitymanagerfactory.createentitymanager () ; Entitymanager.gettransaction (). Begin (); List<event> result = entitymanager.createquery ("From event", Event.class). Getresultlist ();for (Event event : result) {system.out.println ("Event (" + event.getdate () + ") : " + event.gettitle ()); Entitymanager.gettransaction (). commit (); Entitymanager.close ();// The following is the real function to test Enver-version control// To create a different version, take the second record of the database, modify its title and date fields, and cause it to produce version 1 and version 2, which is the following firstrevision and secondrevisionentitymanager = entitymanagerfactory.createentitymanager (); Entitymanager.gettransaction (). Begin (); Event myevent = entitymanager.find (event.class, 2l); Myevent.setdate (New Date ()); Myevent.settitle (Myevent.gettitle () + (rescheduled)); Entitymanager.gettransaction (). Commit () Entitymanager.close ();// Here is a demonstration of how to extract different versions for comparison, similar to SVN versioning, where I don't use JUnit's assert, but I'm used to seeing logger printing Entitymanager = entitymanagerfactory.createentitymanager (); Entitymanager.gettransaction (). Begin (); myEvent = entitymanager.find (EVENT.CLASS,&NBSP;2L);// actually based on the field comparison is to prove that MyEvent and version 2 is the same, and version 1 is not the same auditreader reader = auditreaderfactory.get (Entitymanager); Event firstrevision = reader.find (event.class, 2l, 1); // Version 1--firstrevisionlogger.info ("PRINT:firstRevision.getTitle (). Equals (Myevent.gettitle ())"); Logger.info ( FirstrevisiOn.gettitle () + "|" + myevent.gettitle ()); Logger.info ("PRINT:firstRevision.getDate (). Equals (Myevent.getdate ())"); Logger.info (New simpledateformat ("Yyyy-mm-dd hh:mm:ss"). Format (Firstrevision.getdate ()) + "|" + new simpledateformat ("Yyyy-mm-dd hh:mm:ss"). Format (Myevent.getdate ())); Event secondrevision = reader.find (event.class, 2l, 2); // Version 2--secondrevisionlogger.info ("PRINT:secondRevision.getTitle (). Equals (Myevent.gettitle ())"); Logger.info ( Secondrevision.gettitle () + "|" + myevent.gettitle ()); Logger.info ("PRINT:secondRevision.getDate (). Equals (Myevent.getdate ())"); Logger.info (New simpledateformat ("Yyyy-mm-dd hh:mm:ss"). Format (Secondrevision.getdate ()) + "|" +new simpledateformat ("Yyyy-mm-dd hh:mm:ss"). Format (Myevent.getdate ())); entitymanager.gettransaction (). commit (); Entitymanager.close ();}}
Print results
Hibernate4 using Entitymanager-enver