Envers查询 - 返回REVTYPE以及修改后的字段

时间:2017-04-24 05:13:32

标签: hibernate

我需要在UI上显示对实体进行修改的历史记录。像这样的东西 -

    |-------------------------------------------------------------------------------------------------------------------------------------|
    |Entity name  |  Entity ID  |  Action by  |  Action on           |  Action Type  |  Old value               |  New value              |
    |-------------|-------------|-------------|----------------------|---------------|--------------------------|-------------------------|
    |Person       |     1       |   John Doe  | 2017-04-12 12:00:00  |   Add         |                          | name: Lily, Role: Admin |
    |Person       |     1       |   John Doe  | 2017-04-15 12:00:00  |   Update      |name: Lily                | name: Lily Smith        |
    |Subject      |     5       |   Mary      | 2017-04-17 12:00:00  |   Update      |name: Maths               | name: Mathematics       |
    |Subject      |     6       |   Mary      | 2017-04-17 12:00:00  |   Delete      |name: Science, credits: 5 |                         |
    |-------------------------------------------------------------------------------------------------------------------------------------|

我使用Hibernate envers来存储我需要的所有数据。但是我在阅读所有数据时遇到了问题,以显示我上面需要的内容。我在互联网上搜索了很多博客,问题文章,文档,javadocs,但没有一个显示如何做到这一点。所有这些都显示了如何根据修订版或返回修订版检索实体。除了返回的实体之外,这些实体都没有以下信息 -

  1. 谁做了改变
  2. 什么时候发生了这种变化
  3. 它有什么样的变化。添加/更新/删除
  4. 请注意,我希望尽可能少的查询来加速历史记录API响应。

    我正在使用hibernate和envers版本:' 5.2.9.Final'

    高度赞赏的任何帮助。

    谢谢!

    更新

    这是代码 -

    每个实体类上方的注释 -

    
        @Audited(withModifiedFlag = true)
    
    

    自定义修订监听器 -

    
        public class CustomRevisionListener implements RevisionListener {
    
            private static final Logger logger = 
            LogManager.getLogger(CustomRevisionListener.class);
    
            public void newRevision(Object revisionEntity) {
                logger.info("newRevision, starts revisionEntity="+revisionEntity);
    
                CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
    
                String name = "unknown";
    
                try{
                    //user custom userdetails.User to store id and name both? 
                    //or just store id in string format here?
                    org.springframework.security.core.userdetails.User springUser = 
                            (org.springframework.security.core.userdetails.User)  SecurityContextHolder
                            .getContext().getAuthentication().getPrincipal();
                    name = springUser.getUsername(); //get logged in username
                }catch(Exception e){
                    logger.error("error getting username", e);
                }
    
                logger.info("newRevision, name="+name);
    
                User user = new User();
                user.setName(name);
                revision.setUser(user); //for testing
    
                logger.info("newRevision, setting userId="+user.getId());
            }
        }
    
    

    自定义修订实体 -

    
        @Entity
        @Table(name="REVISIONS")
        @RevisionEntity(CustomRevisionListener.class)
        public class CustomRevisionEntity extends DefaultRevisionEntity {
    
            private static final long serialVersionUID = -6113123831136684807L;
    
            @ManyToOne
            @JoinColumn(name="USER_ID")
            private User user;
    
            public User getUser() {
                return user;
            }
    
            public void setUser(User user) {
                this.user = user;
            }
    
        }
    
    

    envers config -

    
        org.hibernate.envers.track_entities_changed_in_revision=true
        org.hibernate.envers.global_with_modified_flag=true
        org.hibernate.envers.store_data_at_delete=true
        org.hibernate.envers.audit_strategy=org.hibernate.envers.strategy.ValidityAuditStrategy
        org.hibernate.envers.audit_strategy_validity_store_revend_timestamp=true
    
    

    阅读审核 -

    
            public List getAllAudits() {
                logger.debug("getAllAudits starts");
    
                //HERE IS WHERE I NEED HELP.
                //below code is just for trials. I need something which will read all audits and relevant info I described above
    
                ArrayList list = (ArrayList) AuditReaderFactory.get(entityManager) 
                        .createQuery() 
                        .forRevisionsOfEntity(Organization.class, true, true) 
        //              .add(AuditEntity.id().eq(bitacoraControlId)) 
                        .addOrder(AuditEntity.revisionNumber().asc()) 
                        .getResultList();
    
                for(int i=0; i  revisions = reader.getRevisions(Organization.class, 2);
                for(Number revNum:revisions){
                    Organization article =reader.find(Organization.class, 2, revNum);
                    System.out.println("Revision No: " + revNum);
                    System.out.println("Title: " + article.getId());
                    System.out.println("Content: " + article.getName());        
    
                }
    
                logger.debug("getAllAudits list size="+list.size());
    
                return list;
            }
    
    

0 个答案:

没有答案