快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡亰app在线下载:JBoss Envers学习笔记



试用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 da澳门新葡亰app在线下载ta without much effort.

JBoss Envers目的是根据对实体的设置,供给记录履行数据变化历史的功能(数据变化版本)。Envers的设置设置设备摆设摆设异常简单,假如必要澳门新葡亰app在线下载对某个实例进行历史数据版本记录,只必要在实例上设置设置设备摆设摆设@Versioned annotation即可。 针对每个实体的版本的历史数据,Envers都邑创建一个零丁的数据表进行存储。

今澳门新葡亰app在线下载朝Envers支持Hibernate和Hibernate-entitymanager(JPA实现)

本示例以Hibernate-entitymanager为例,解说其设置设置设备摆设摆设的措施:

先设置设置设备摆设摆设 persistence.xml, 加入 property设置设置设备摆设摆设

persistence-unit >

provider>org.hibernate.ejb.HibernatePersistenceprovider>

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>

示例代码:

1import org.jboss.versions.Versioned;

2

3import javax.persistence.Entity;

4import javax.persistence.Id;

5import javax.persistence.GeneratedValue;

6import javax.persistence.Column;

7

8@Entity

9@Versioned

10public class Blog {

11@Id

12@Column(length=32)

13private String id;

14

15@Versioned

16@Column(length=100)

17private String title;

18

19@Column(length=2000)

20private String date;

21

22@Versioned

23@ManyToOne

24private String body;

25

26@ManyToOne

27private Author author;

28// add getters, setters, constructors, equals and hashCode here

29}

30

31@Entity

32@Versioned

33public class Author {

34

35@Id

36@Column(length=32)

37private String id;

38

39@Versioned

40@Column(length=20)

41private String name;

42

43 }

下面是进行测试的代码:

1

// 新增操作

entityManager.getTransaction().begin();

2

3Author matthew = new Author("1", "Matthew Xie");

4Blog newBlog = new Blog("1", "Matthew's new Blog" "TODO{add content here}", matthew);

5

6entityManager.persist(matthew);

7entityManager.persist(newBlog);

8

9entityManager.getTransaction().commit();

1//对Blog和author进行改动操作

2entityManager.getTransaction().begin();

3

4Author author = entityManager.find(Author.class, "1");

5Blog blog = entityManager.find(Blog.class, "1");

6

7// Changing the address's house number

8author.setName("Matt Xie")

9

10Author newAuthor = new Author("2", "newAuthor");

11

12// change blog author to newAuthor

13blog.setAuthor(newAuthor);

14

15entityManager.getTransaction().commit();

1//下面代码,演示了若何取得历史版本数据

2VersionsReader reader = VersionsReaderFactory.get(entityManager);

3

4// get Blog all versions id

5ListNumber> versions = reader.getRevisions(Blog.class, "1"/*blog id*/);

6for (Number version : versions) {

7Blog 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澳门新葡亰app在线下载 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 澳门新葡亰app在线下载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).

您可能还会对下面的文章感兴趣: