<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 156,  comments - 601,  trackbacks - 0
        試用JBoss Envers項(xiàng)目有一陣子了,趁Envers項(xiàng)目發(fā)布 1.1.0版,也同時(shí)把學(xué)習(xí)筆記共享給大家,希望對(duì)大家有所幫助。
        下面來看一下JBoss Envers項(xiàng)目的目的,官方說明如下:
    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目的是根據(jù)對(duì)實(shí)體的設(shè)置,提供記錄執(zhí)行數(shù)據(jù)變更歷史的功能(數(shù)據(jù)變更版本)。Envers的配置非常簡(jiǎn)單,如果需要對(duì)某個(gè)實(shí)例進(jìn)行歷史數(shù)據(jù)版本記錄,只需要在實(shí)例上配置@Versioned annotation即可。 針對(duì)每個(gè)實(shí)體的版本的歷史數(shù)據(jù),Envers都會(huì)創(chuàng)建一個(gè)單獨(dú)的數(shù)據(jù)表進(jìn)行存儲(chǔ)。
    目前Envers支持Hibernate和Hibernate-entitymanager(JPA實(shí)現(xiàn))

    本示例以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>

    示例代碼:
     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 }    

    下面是進(jìn)行測(cè)試的代碼:
    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();

     1  //對(duì)Blog和author進(jìn)行修改操作
     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  //下面代碼,演示了如何取得歷史版本數(shù)據(jù)
    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  }




    注: 補(bǔ)充 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官方網(wǎng)址: http://www.jboss.org/envers

    Good Luck!
    Yours Matthew!
    posted on 2008-11-14 12:14 x.matthew 閱讀(1873) 評(píng)論(1)  編輯  收藏 所屬分類: 最新開源動(dòng)態(tài)Spring|Hibernate|Other framework
    主站蜘蛛池模板: 免费看黄福利app导航看一下黄色录像| 亚洲男人av香蕉爽爽爽爽| 亚洲不卡av不卡一区二区| 人人爽人人爽人人片av免费| 国产一区二区三区在线免费观看| 亚洲一区二区三区在线网站| 18禁网站免费无遮挡无码中文| 亚洲av无码片在线观看| 岛国av无码免费无禁网站| 亚洲AV无码XXX麻豆艾秋| 又大又硬又爽免费视频| www在线观看播放免费视频日本| 亚洲人成网亚洲欧洲无码久久| 国产在线观a免费观看| 亚洲成人免费在线| 男女超爽刺激视频免费播放| 色在线亚洲视频www| 永久免费看bbb| 一区二区三区免费在线观看| 亚洲精品无码专区在线在线播放| 久久免费区一区二区三波多野| 亚洲精彩视频在线观看| 成年女人免费碰碰视频| 美女裸免费观看网站| 亚洲熟妇无码乱子AV电影 | 香蕉视频免费在线| 亚洲伊人久久精品影院| 久久福利资源网站免费看| 亚洲第一成年免费网站| 自拍偷自拍亚洲精品情侣| 永久黄色免费网站| 亚洲AV成人无码网天堂| 亚洲成AV人片在线观看无码| 在线天堂免费观看.WWW| 国内成人精品亚洲日本语音| 亚洲国产成人片在线观看无码| 国产无人区码卡二卡三卡免费| 成人嫩草影院免费观看| 亚洲日本在线免费观看| 亚洲第一黄色网址| free哆啪啪免费永久|