- 浏览: 313070 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
mrcuya1:
这段代码貌似有点问题.BeanAttributesMapper ...
使用 Spring LDAP 读取数据并映射到 Java Bean 中 -
SSailYang:
xcoder 写道请问使用gradle开发osgi项目,怎么对 ...
Gradle 实践 -
xcoder:
请问使用gradle开发osgi项目,怎么对代码进行调试啊?
Gradle 实践 -
lihc_sd0531:
学习啦
LDAP 中 CN, OU, DC 的含义 -
SSailYang:
chenlejia 写道用它怎么做时间段的查询这个显然没法做, ...
颇为实用的 Hibernate Example 增强版
下文转自:http://www.blogjava.net/xmatthew/archive/2008/11/14/238432.html
试用JBoss Envers项目有一阵子了,趁Envers项目发布 1.1.0版,也同时把学习笔记共享给大家,希望对大家有所帮助。
下面来看一下JBoss Envers项目的目的,官方说明如下:
<!---->The Envers project aims to enable easy versioning of persistent classes.
All that you have to do is annotate your persistent class or some of its properties,
that you want to version, with @Versioned. For each versioned entity, a table will be created,
which will hold the history of changes made to the entity. You can then retrieve and
query historical data without much effort.
All that you have to do is annotate your persistent class or some of its properties,
that you want to version, with @Versioned. For each versioned entity, a table will be created,
which will hold the history of changes made to the entity. You can then retrieve and
query historical data without much effort.
JBoss
Envers目的是根据对实体的设置,提供记录执行数据变更历史的功能(数据变更版本)。Envers的配置非常简单,如果需要对某个实例进行历史数据版
本记录,只需要在实例上配置@Versioned annotation即可。
针对每个实体的版本的历史数据,Envers都会创建一个单独的数据表进行存储。
目前Envers支持Hibernate和Hibernate-entitymanager(JPA实现)
本示例以Hibernate-entitymanager为例,讲解其配置的方法:
先配置 persistence.xml, 加入 property配置
<!---->
<
persistence-unit
>
< provider > org.hibernate.ejb.HibernatePersistence </ provider >
< class > </ class >
< properties >
< property name ="hibernate.dialect" />
<!-- other hibernate properties -->
< property name ="hibernate.ejb.event.post-insert"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.post-update"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.post-delete"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.pre-collection-update"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.pre-collection-remove"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.post-collection-recreate"
value ="org.jboss.envers.event.VersionsEventListener" />
</ properties >
</ persistence-unit >
< provider > org.hibernate.ejb.HibernatePersistence </ provider >
< class > </ class >
< properties >
< property name ="hibernate.dialect" />
<!-- other hibernate properties -->
< property name ="hibernate.ejb.event.post-insert"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.post-update"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.post-delete"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.pre-collection-update"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.pre-collection-remove"
value ="org.jboss.envers.event.VersionsEventListener" />
< property name ="hibernate.ejb.event.post-collection-recreate"
value ="org.jboss.envers.event.VersionsEventListener" />
</ properties >
</ persistence-unit >
示例代码:
<!----> 1
import
org.jboss.versions.Versioned;
2
3 import javax.persistence.Entity;
4 import javax.persistence.Id;
5 import javax.persistence.GeneratedValue;
6 import javax.persistence.Column;
7
8 @Entity
9 @Versioned
10 public class Blog {
11 @Id
12 @Column(length = 32 )
13 private String id;
14
15 @Versioned
16 @Column(length = 100 )
17 private String title;
18
19 @Column(length = 2000 )
20 private String date;
21
22 @Versioned
23 @ManyToOne
24 private String body;
25
26 @ManyToOne
27 private Author author;
28 // add getters, setters, constructors, equals and hashCode here
29 }
30
31 @Entity
32 @Versioned
33 public class Author {
34
35 @Id
36 @Column(length = 32 )
37 private String id;
38
39 @Versioned
40 @Column(length = 20 )
41 private String name;
42
43 }
2
3 import javax.persistence.Entity;
4 import javax.persistence.Id;
5 import javax.persistence.GeneratedValue;
6 import javax.persistence.Column;
7
8 @Entity
9 @Versioned
10 public class Blog {
11 @Id
12 @Column(length = 32 )
13 private String id;
14
15 @Versioned
16 @Column(length = 100 )
17 private String title;
18
19 @Column(length = 2000 )
20 private String date;
21
22 @Versioned
23 @ManyToOne
24 private String body;
25
26 @ManyToOne
27 private Author author;
28 // add getters, setters, constructors, equals and hashCode here
29 }
30
31 @Entity
32 @Versioned
33 public class Author {
34
35 @Id
36 @Column(length = 32 )
37 private String id;
38
39 @Versioned
40 @Column(length = 20 )
41 private String name;
42
43 }
下面是进行测试的代码:
<!---->1
// 新增操作
entityManager.getTransaction().begin();
2
3 Author matthew = new Author( " 1 " , " Matthew Xie " );
4 Blog newBlog = new Blog( " 1 " , " Matthew's new Blog " " TODO{add content here} " , matthew);
5
6 entityManager.persist(matthew);
7 entityManager.persist(newBlog);
8
9 entityManager.getTransaction().commit();
// 新增操作
entityManager.getTransaction().begin();
2
3 Author matthew = new Author( " 1 " , " Matthew Xie " );
4 Blog newBlog = new Blog( " 1 " , " Matthew's new Blog " " TODO{add content here} " , matthew);
5
6 entityManager.persist(matthew);
7 entityManager.persist(newBlog);
8
9 entityManager.getTransaction().commit();
<!----> 1
//
对Blog和author进行修改操作
2 entityManager.getTransaction().begin();
3
4 Author author = entityManager.find(Author. class , " 1 " );
5 Blog blog = entityManager.find(Blog. class , " 1 " );
6
7 // Changing the address's house number
8 author.setName( " Matt Xie " )
9
10 Author newAuthor = new Author( " 2 " , " newAuthor " );
11
12 // change blog author to newAuthor
13 blog.setAuthor(newAuthor);
14
15 entityManager.getTransaction().commit();
2 entityManager.getTransaction().begin();
3
4 Author author = entityManager.find(Author. class , " 1 " );
5 Blog blog = entityManager.find(Blog. class , " 1 " );
6
7 // Changing the address's house number
8 author.setName( " Matt Xie " )
9
10 Author newAuthor = new Author( " 2 " , " newAuthor " );
11
12 // change blog author to newAuthor
13 blog.setAuthor(newAuthor);
14
15 entityManager.getTransaction().commit();
<!---->1
//
下面代码,演示了如何取得历史版本数据
2 VersionsReader reader = VersionsReaderFactory.get(entityManager);
3
4 // get Blog all versions id
5 List < Number > versions = reader.getRevisions(Blog. class , " 1 " /* blog id */ );
6 for (Number version : versions) {
7 Blog blog = reader.find(Blog. class , " 1 " , version);
8 }
2 VersionsReader reader = VersionsReaderFactory.get(entityManager);
3
4 // get Blog all versions id
5 List < Number > versions = reader.getRevisions(Blog. class , " 1 " /* blog id */ );
6 for (Number version : versions) {
7 Blog blog = reader.find(Blog. class , " 1 " , version);
8 }
注: 补充 Hibernate Envers的Property配置说明
Property name | Default value | Description |
org.jboss.envers.versionsTablePrefix | String that will be prepended to the name of a versioned entity to create the name of the entity, that will hold version information. | |
org.jboss.envers.versionsTableSuffix | _versions | String that will be appended to the name of a versioned entity
to create the name of the entity, that will hold version information. If you version
an entity with a table name Person
, in the default setting Envers will
generate a Person_versions
table to store historical data.
|
org.jboss.envers.revisionFieldName | _revision | Name of a field in the versions entity that will hold the revision number. |
org.jboss.envers.revisionTypeFieldName | _revision_type | Name of a field in the versions entity that will hold the type of the revision (currently, this can be: add, mod, del). |
org.jboss.envers.revisionOnCollectionChange | true | Should a revision be generated when a not-owned relation field changes (this can be either a collection in a one-to-many relation, or the field using "mappedBy" attribute in a one-to-one relation). |
org.jboss.envers.warnOnUnsupportedTypes | false | When true, a warning in the log will be issued when a property is versioned with an unsupported type, instead of an exception. This way, the configuration process isn't interrupted, but the version schema isn't complete (it lacks the unsupported properties, which won't be versioned). |
org.jboss.envers.unversionedOptimisticLockingField | false | When true, properties to be used for optimistic locking, annotated with @Version, will be automatically unversioned (their history won't be stored; it normally doesn't make sense to store it). |
JBoss Envers官方网址: http://www.jboss.org/envers
Good Luck!
Yours Matthew!
发表评论
-
一个 Java SE 7 Fork/Join 的小例子
2012-12-30 17:48 851使用 Java SE 7 Fork/Join 的关键是要是 f ... -
在 IntelliJ IDEA 中加快 Maven 项目的单元测试编译速度
2011-10-31 10:03 6003IntelliJ IDEA 是一个很棒的 IDE,它有很多 E ... -
SLF4J 与 Log4J,以及何时使用 isDebugEnabled 判断
2011-10-28 09:25 6587之前一篇关于 SLF4J 和 Log4J 的文章有不当之处,S ... -
The reason of ServiceMix cannot start up after install CollabNet Subversion Edge
2011-09-08 14:40 1165What's the problem? Today ... -
新技术介绍:Hades and Spring Data
2011-02-16 09:43 1887Recently I read some articl ... -
JAXB unmarshall
2011-02-09 15:31 1082在使用 JAXB 将 XML umarshall 为 Java ... -
配置 iBatis TypeHandler 时遇到的一个问题
2010-09-30 14:49 3632需要使用 iBatis 将对象序列化到一个表的 BLOB 字段 ... -
Search OSGi Bundle
2010-09-20 11:13 710找 OSGi Bundle,到 http://repo2.ma ... -
iBatis 返回自动生成的主键的问题
2010-09-20 11:07 1326想让 iBatis insert 返回生成的主键的话还要在 s ... -
总结在 ServiceMix 中遇到 ClassNotFoundException 的原因
2010-07-02 09:43 0在 ServiceMix 中,运行 Bundle 时遇到 Cl ... -
使用 Felix Maven Bundle 插件将 Jar 包打入到 OSGi bundle 中
2010-06-28 14:34 3983在开发 OSGi bundle 时,如果你的 Bundle 所 ... -
总结使用 SericeMix 遇到的问题
2010-06-27 16:37 1164现在的项目使用 ServiceMix 作为运行环境,由 ... -
[转] 高扩展WEB应用HTTP SESSION共享方案
2010-04-21 21:09 937www.yeeach.com/2010/03/27/高扩展we ... -
简单用了一下 VisualVM
2010-04-17 13:21 1182原来分析程序性能用的是 YourKit(其实是别人用,自己看分 ... -
Java Classloader
2010-03-09 15:48 883看了 IBM developerWorks 上的“深入探讨 J ... -
NIO 文件随机存取问题
2010-03-07 20:11 830NIO 的内存映射文件机制虽然在操作大文件上有速度的优势,但我 ... -
了解事务陷阱
2009-08-23 12:00 944读了 IBM developerWorks 上的文章:“事务策 ... -
LambdaJ
2009-06-23 15:21 2523Lambda = λ LambdaJ 的主要目的是简化对集合的 ... -
错误地理解了 Boolean.parseBoolean 的用法
2009-03-05 10:35 5961写程序,用到了 Boolean.parseBoolean 方法 ... -
Web Beans and the EE platform
2008-11-11 22:48 1174http://in.relation.to/Bloggers/ ...
相关推荐
【JBoss7学习笔记】 JBoss Application Server,简称JBoss AS,是Red Hat公司开发的一款开源Java EE应用服务器,而JBoss7则是其一个重要的版本,带来了许多性能优化和架构改进。这篇学习笔记将深入探讨JBoss7的核心...
综上所述,本项目结合了JPA进行数据操作,利用Jboss Envers进行数据审计,以及JasperReport和iReport进行报表设计和展示,构建了一个功能丰富的信息系统。这些技术的集成可以提供数据管理、历史追踪以及可视化的报表...
JBossESB学习笔记 收集了网上1-16系列教程,笔记详细介绍了JBossESB各个组件的特性及配置文件的说明
### JBoss ESB 学习笔记知识点概览 #### 一、搭建ESB开发环境 - **工具准备**: - Eclipse-JEE 3.5:集成开发环境,支持Java EE标准,适合企业级应用程序开发。 - jbossesb-server-4.7:JBoss ESB的具体版本,为...
【JBoss ESB 学习笔记】 JBoss ESB(Enterprise Service Bus,企业服务总线)是Red Hat公司开发的一款开源服务导向架构(SOA)平台,它为分布式应用程序提供了集成和互操作性。本笔记将深入探讨JBoss ESB的核心概念...
【JBoss ESB学习笔记】 JBoss ESB(Enterprise Service Bus)是Red Hat公司开发的一款开源企业服务总线,它是企业级应用集成的核心组件,用于连接不同系统、服务和应用程序,实现服务之间的通信和交互。本学习笔记...
本篇笔记将详细介绍如何搭建JBoss ESB的开发环境。 首先,我们需要准备的是Eclipse IDE,这里推荐使用Eclipse-JEE 3.5版本,因为该版本对Java EE开发有着良好的支持,同时包含了对各种服务器的集成。如果你还没有...
4. **Spring整合**:《JBoss_ESB学习笔记13——第十个ESB应用Spring_AOP.doc》和《JBoss_ESB学习笔记12——第十个ESB应用Spring_helloworld.doc》涉及到Spring框架与ESB的结合,Spring AOP(面向切面编程)在ESB中的...
### jBoss + Tomcat 学习笔记大全及帮助文档概览 #### 一、环境搭建与配置 在开始深入探讨jBoss与Tomcat的集成之前,我们先来了解如何搭建基本的开发环境。 ##### 1. Java 环境配置 - **安装 J2SDK1.4+ 和 J2...
本篇学习笔记主要围绕 JBoss ESB 的一个基础应用——“Hello World File Action”进行讲解,这个例子展示了如何利用 JBoss ESB 的 File Gateway 功能来监控文件系统变化,并通过 JMS(Java Message Service)消息...
【JBoss学习全集多本书整合】集合了丰富的JBoss相关知识,涵盖了JBoss服务器的配置、管理和优化等多个方面,是深入理解与掌握JBoss的理想资料。JBoss,作为一个开源的应用服务器,基于Java EE(现在被称为Jakarta EE...
NULL 博文链接:https://siye1982.iteye.com/blog/592400
在windows 下对于jboss7配置的学习ppt,包括如何建立一个Datasource
在JBoss文档中,我们可以深入学习以下几个关键知识点: 1. **Java Enterprise Edition (Java EE) 基础**:Java EE是企业级应用开发的标准框架,包含了一系列接口和实现,如Servlet、JSP、EJB等,用于构建可扩展且...
Jboss 项目部署文档 Jboss 项目部署文档是指在 Jboss 服务器上部署项目的详细步骤,包括环境变量的配置、项目打包、配置文件的修改、JNDI 的配置等。以下是 Jboss 项目部署文档的详细知识点: 一、环境变量配置 ...
【JBOSS,JBoss安装部署】 ...以上就是JBoss安装和部署的基本步骤,深入使用还需要学习更多高级特性,如模块化、集群、数据源配置等。通过不断实践和学习,你将能够更好地掌握和运用JBoss来支撑你的Java EE应用程序。