試用JBoss Envers項目有一陣子了趁Envers項目發布 版也同時把學習筆記共享給大家希望對大家有所幫助
下面來看一下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
JBoss Envers目的是根據對實體的設置提供記錄執行數據變更歷史的功能(數據變更版本)Envers的配置非常簡單如果需要對某個實例進行歷史數據版本記錄只需要在實例上配置@Versioned annotation即可 針對每個實體的版本的歷史數據Envers都會創建一個單獨的數據表進行存儲
目前Envers支持Hibernate和Hibernateentitymanager(JPA實現)
本示例以Hibernateentitymanager為例講解其配置的方法
先配置 persistencexml 加入 property配置
<persistenceunit >
<provider>orghibernateejbHibernatePersistence</provider>
<class></class>
<properties>
<property name=hibernatedialect />
<! other hibernate properties >
<property name=hibernateejbeventpostinsert
value=orgjbossenverseventVersionsEventListener />
<property name=hibernateejbeventpostupdate
value=orgjbossenverseventVersionsEventListener />
<property name=hibernateejbeventpostdelete
value=orgjbossenverseventVersionsEventListener />
<property name=hibernateejbeventprecollectionupdate
value=orgjbossenverseventVersionsEventListener />
<property name=hibernateejbeventprecollectionremove
value=orgjbossenverseventVersionsEventListener />
<property name=hibernateejbeventpostcollectionrecreate
value=orgjbossenverseventVersionsEventListener />
</properties>
</persistenceunit>
示例代碼
import orgjbossversionsVersioned;
import javaxpersistenceEntity;
import javaxpersistenceId;
import javaxpersistenceGeneratedValue;
import javaxpersistenceColumn;
@Entity
@Versioned
public class Blog {
@Id
@Column(length=)
private String id;
@Versioned
@Column(length=)
private String title;
@Column(length=)
private String date;
@Versioned
@ManyToOne
private String body;
@ManyToOne
private Author author;
// add getters setters constructors equals and hashCode here
}
@Entity
@Versioned
public class Author {
@Id
@Column(length=)
private String id;
@Versioned
@Column(length=)
private String name;
}
下面是進行測試的代碼
// 新增操作
entityManagergetTransaction()begin();
Author matthew = new Author( Matthew Xie);
Blog newBlog = new Blog( Matthews new Blog TODO{add content here} matthew);
entityManagerpersist(matthew);
entityManagerpersist(newBlog);
entityManagergetTransaction(mit();
//對Blog和author進行修改操作
entityManagergetTransaction()begin();
Author author = entityManagerfind(Authorclass );
Blog blog = entityManagerfind(Blogclass );
// Changing the addresss house number
authorsetName(Matt Xie)
Author newAuthor = new Author( newAuthor);
// change blog author to newAuthor
blogsetAuthor(newAuthor);
entityManagergetTransaction(mit();
//下面代碼演示了如何取得歷史版本數據
VersionsReader reader = VersionsReaderFactoryget(entityManager);
// get Blog all versions id
List<Number> versions = readergetRevisions(Blogclass /*blog id*/);
for (Number version : versions) {
Blog blog = readerfind(Blogclass version);
}
注 補充 Hibernate Envers的Property配置說明
Property name
Default value
Description
orgjbossenversversionsTablePrefix
String that will be prepended to the name of a versioned entity to create the name of the entity that will hold version information
orgjbossenversversionsTableSuffix
_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
orgjbossenversrevisionFieldName
_revision
Name of a field in the versions entity that will hold the revision number
orgjbossenversrevisionTypeFieldName
_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)
orgjbossenversrevisionOnCollectionChange
true
Should a revision be generated when a notowned relation field changes (this can be either a collection in a onetomany relation or the field using mappedBy attribute in a onetoone relation)
orgjbossenverswarnOnUnsupportedTypes
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 isnt interrupted but the version schema isnt complete (it lacks the unsupported properties which wont be versioned)
orgjbossenversunversionedOptimisticLockingField
false
When true properties to be used for optimistic locking annotated with @Version will be automatically unversioned (their history wont be stored; it normally doesnt make sense to store it)
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28219.html