标签:
简而言之,Enver就是版本控制,记录数据库操作,控制粒度为字段,原理类似于SVN
1.pom.xml配置和Event.java的定义参考http://my.oschina.net/u/555061/blog/506052
2.目录结构有所不同
Enver是采用映射xml配置的,原来的映射配置为hibernate.cfg.xml,现在为persistence.xml,且persistence.xml所在目录为META-INF的下一级(目录的问题应该可以配置,但本人暂时未作研究)
3.查看下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 name="hibernate.hbm2ddl.auto" value="create" /> </properties> </persistence-unit> </persistence>
改变也不是太大,主要就是
<mapping class="org.hibernate.tutorial.annotations.Event"/>
改为了
<class>org.hibernate.tutorial.envers.Event</class>
其实也就是写法规定而已,问题不大
4.配置问题已经不是主要问题,版本控制查看应用是主要问题,如下
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()); private EntityManagerFactory entityManagerFactory; @Override protected void setUp() throws Exception { entityManagerFactory = Persistence .createEntityManagerFactory("org.hibernate.tutorial.envers"); } @Override protected void tearDown() throws Exception { entityManagerFactory.close(); } public void testBasicUsage() { // 这里主要演示如何使用EntityManagerFactory的这种方式去操作数据库,插入2条数据 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(); // 这里就是演示如何用EntityManagerFactory从数据库查询出对象list entityManager = 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(); // 下面就是要测试Enver的真正功能--版本控制 // 为了创建不同的版本,这里取出数据库第二条记录,修改它的title和date字段,导致其产生版本1和版本2,也就是下面的firstRevision和secondRevision entityManager = 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(); // 这里就是演示如何抽取不同的版本进行比较,类似于SVN版本控制的对比,这里我不使用JUNIT的assert,而是习惯看logger打印 entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); myEvent = entityManager.find(Event.class, 2L);// 其实根据字段的比较就是想证明myEvent跟版本2是一样的,跟版本1是不一样的 AuditReader reader = AuditReaderFactory.get(entityManager); Event firstRevision = reader.find(Event.class, 2L, 1); //版本1--firstRevision logger.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); //版本2--secondRevision logger.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(); } }
打印结果
hibernate4使用EntityManager-Enver
标签:
原文地址:http://my.oschina.net/u/555061/blog/506106