试用JBoss Envers项目有一阵子了趁Envers项目发布 版也同时把学习笔记共享给大家希望对大家有所帮助
下面来看一下JBoss Envers项目的目的官方说明如下
TheEnversprojectaimstoenableeasyversioningofpersistentclasses
Allthatyouhavetodoisannotateyourpersistentclassorsomeofitsproperties
thatyouwanttoversionwith@VersionedForeachversionedentityatablewillbecreated
whichwillholdthehistoryofchangesmadetotheentityYoucanthenretrieveand
queryhistoricaldatawithoutmucheffort
JBoss Envers目的是根据对实体的设置提供记录执行数据变更历史的功能(数据变更版本)Envers的配置非常简单如果需要对某个实例进行历史数据版本记录只需要在实例上配置@Versioned annotation即可 针对每个实体的版本的历史数据Envers都会创建一个单独的数据表进行存储
目前Envers支持Hibernate和Hibernateentitymanager(JPA实现)
本示例以Hibernateentitymanager为例讲解其配置的方法
先配置 persistencexml 加入 property配置
<persistenceunit>
<provider>orghibernateejbHibernatePersistence</provider>
<class></class>
<properties>
<propertyname=hibernatedialect/>
<!otherhibernateproperties>
<propertyname=hibernateejbeventpostinsert
value=orgjbossenverseventVersionsEventListener/>
<propertyname=hibernateejbeventpostupdate
value=orgjbossenverseventVersionsEventListener/>
<propertyname=hibernateejbeventpostdelete
value=orgjbossenverseventVersionsEventListener/>
<propertyname=hibernateejbeventprecollectionupdate
value=orgjbossenverseventVersionsEventListener/>
<propertyname=hibernateejbeventprecollectionremove
value=orgjbossenverseventVersionsEventListener/>
<propertyname=hibernateejbeventpostcollectionrecreate
value=orgjbossenverseventVersionsEventListener/>
</properties>
</persistenceunit>
示例代码
importorgjbossversionsVersioned;
importjavaxpersistenceEntity;
importjavaxpersistenceId;
importjavaxpersistenceGeneratedValue;
importjavaxpersistenceColumn;
@Entity
@Versioned
publicclassBlog{
@Id
@Column(length=)
privateStringid;
@Versioned
@Column(length=)
privateStringtitle;
@Column(length=)
privateStringdate;
@Versioned
@ManyToOne
privateStringbody;
@ManyToOne
privateAuthorauthor;
//addgetterssettersconstructorsequalsandhashCodehere
}
@Entity
@Versioned
publicclassAuthor{
@Id
@Column(length=)
privateStringid;
@Versioned
@Column(length=)
privateStringname;
}
下面是进行测试的代码
// 新增操作
entityManagergetTransaction()begin();
Authormatthew=newAuthor(MatthewXie);
BlognewBlog=newBlog(MatthewsnewBlogTODO{addcontenthere}matthew);
entityManagerpersist(matthew);
entityManagerpersist(newBlog);
entityManagergetTransaction(mit();
//对Blog和author进行修改操作
entityManagergetTransaction()begin();
Authorauthor=entityManagerfind(Authorclass);
Blogblog=entityManagerfind(Blogclass);
//Changingtheaddressshousenumber
authorsetName(MattXie)
AuthornewAuthor=newAuthor(newAuthor);
//changeblogauthortonewAuthor
blogsetAuthor(newAuthor);
entityManagergetTransaction(mit();
//下面代码演示了如何取得历史版本数据
VersionsReaderreader=VersionsReaderFactoryget(entityManager);
//getBlogallversionsid
List<Number>versions=readergetRevisions(Blogclass/*blogid*/);
for(Numberversion:versions){
Blogblog=readerfind(Blogclassversion);
}
注 补充 Hibernate Envers的Property配置说明
Property nameDefault valueDescriptionorgjbossenversversionsTablePrefix
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_versionsString 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_revisionName of a field in the versions entity that will hold the revision numberorgjbossenversrevisionTypeFieldName_revision_typeName of a field in the versions entity that will hold the type of the revision (currently this can be: add mod del)orgjbossenversrevisionOnCollectionChangetrueShould 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)orgjbossenverswarnOnUnsupportedTypesfalseWhen 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)orgjbossenversunversionedOptimisticLockingFieldfalseWhen 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)