- 浏览: 307941 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
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 811使用 Java SE 7 Fork/Join 的关键是要是 f ... -
在 IntelliJ IDEA 中加快 Maven 项目的单元测试编译速度
2011-10-31 10:03 5953IntelliJ IDEA 是一个很棒的 IDE,它有很多 E ... -
SLF4J 与 Log4J,以及何时使用 isDebugEnabled 判断
2011-10-28 09:25 6526之前一篇关于 SLF4J 和 Log4J 的文章有不当之处,S ... -
The reason of ServiceMix cannot start up after install CollabNet Subversion Edge
2011-09-08 14:40 1129What's the problem? Today ... -
新技术介绍:Hades and Spring Data
2011-02-16 09:43 1849Recently I read some articl ... -
JAXB unmarshall
2011-02-09 15:31 1020在使用 JAXB 将 XML umarshall 为 Java ... -
配置 iBatis TypeHandler 时遇到的一个问题
2010-09-30 14:49 3588需要使用 iBatis 将对象序列化到一个表的 BLOB 字段 ... -
Search OSGi Bundle
2010-09-20 11:13 670找 OSGi Bundle,到 http://repo2.ma ... -
iBatis 返回自动生成的主键的问题
2010-09-20 11:07 1246想让 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 3948在开发 OSGi bundle 时,如果你的 Bundle 所 ... -
总结使用 SericeMix 遇到的问题
2010-06-27 16:37 1129现在的项目使用 ServiceMix 作为运行环境,由 ... -
[转] 高扩展WEB应用HTTP SESSION共享方案
2010-04-21 21:09 901www.yeeach.com/2010/03/27/高扩展we ... -
简单用了一下 VisualVM
2010-04-17 13:21 1125原来分析程序性能用的是 YourKit(其实是别人用,自己看分 ... -
Java Classloader
2010-03-09 15:48 851看了 IBM developerWorks 上的“深入探讨 J ... -
NIO 文件随机存取问题
2010-03-07 20:11 792NIO 的内存映射文件机制虽然在操作大文件上有速度的优势,但我 ... -
了解事务陷阱
2009-08-23 12:00 905读了 IBM developerWorks 上的文章:“事务策 ... -
LambdaJ
2009-06-23 15:21 2481Lambda = λ LambdaJ 的主要目的是简化对集合的 ... -
错误地理解了 Boolean.parseBoolean 的用法
2009-03-05 10:35 5901写程序,用到了 Boolean.parseBoolean 方法 ... -
Web Beans and the EE platform
2008-11-11 22:48 1141http://in.relation.to/Bloggers/ ...
相关推荐
自己辛苦整理的网上的JBoss ESB学习笔记 ,非常详细,代码操作都有截图; 希望大家多支持! 学习笔记PDF的目录如下: 1——搭建ESB开发环境 2 2——第一个ESB代码Hello World 12 3——第二个ESB代码Hello World ...
JPA配置装载 Jboss Envers Jasperreport ireport 项目源码 博文链接:https://neil-jh.iteye.com/blog/235838
JBossESB学习笔记 收集了网上1-16系列教程,笔记详细介绍了JBossESB各个组件的特性及配置文件的说明
NULL 博文链接:https://yang-min.iteye.com/blog/1954854
Jboss_ESB学习笔记以及总结和实例
Jboss_ESB学习笔记,介绍的很详细,可以作为入门文档来看。
Jboss_ESB学习笔记.doc
jBoss+tomcat学习笔记大全,jBoss+tomcat的帮助文档
。。。
NULL 博文链接:https://siye1982.iteye.com/blog/592400
是JBoss学习的最佳材料,包含JBoss的配置以及其文件修改方法和文件介绍。
JBOSS使用指南JBOSS使用指南JBOSS使用指南JBOSS使用指南JBOSS使用指南JBOSS使用指南JBOSS使用指南JBOSS使用指南
在windows 下对于jboss7配置的学习ppt,包括如何建立一个Datasource
个人的一些学习记录,大家可以做个参考。 安装 启动 部署 端口修改 访问jboss 连接池配置 连接池验证 JVM内存 连接池参数 线程参数
JBoss Rules 的前身是Codehaus的一个开源项目叫Drools。最近被纳入JBoss门下,更名为JBoss Rules,成为了JBoss应用服务器的规则引擎。 Drools是为Java量身定制的基于Charles Forgy的RETE算法的规则引擎的实现。具有...
jboss rules 中文学习资料.chm
JBOSS_Esb学习资料包含各种基础学习资料与协议交互资料
JBOSS,JBoss安装部署 JBOSS,JBoss安装部署
JBoss启动JBoss启动JBoss启动JBoss启动JBoss启动JBoss启动JBoss启动JBoss启动 JBoss启动