下文转自:http://www.blogjava.net/xmatthew/archive/2008/11/14/238432.html
试用JBoss Envers项目有一阵子了,趁Envers项目发布 1.1.0版,也同时把学习笔记共享给大家,希望对大家有所帮助。
下面来看一下JBoss Envers项目的目的,官方说明如下:
JBoss Envers目的是根据对实体的设置,提供记录执行数据变更历史的功能(数据变更版本)。Envers的配置非常简单,如果需要对某个实例进行历史数据版 本记录,只需要在实例上配置@Versioned annotation即可。 针对每个实体的版本的历史数据,Envers都会创建一个单独的数据表进行存储。
目前Envers支持Hibernate和Hibernate-entitymanager(JPA实现)
本示例以Hibernate-entitymanager为例,讲解其配置的方法:
先配置 persistence.xml, 加入 property配置
示例代码:
下面是进行测试的代码:
注: 补充 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!