【问题标题】:Audits with Spring Data Neo4j使用 Spring Data Neo4j 进行审计
【发布时间】:2012-11-15 06:49:48
【问题描述】:

我目前正在开发一个使用 Spring Data Neo4j 的项目。每当创建 NodeEntity 时,我都想创建一个引用的 Audit NodeEntity,其中包含创建日期和用户。

我想出的一个解决方案是编写一个 AOP Aspect,它与我的服务层的 create 方法挂钩。这适用于没有级联的实体,但是级联的实体呢?这没有在我的服务层中显式传递,因此我的 AOP 类不会拦截它们。 JPA 中是否有类似实体侦听器的概念,或者我该如何挂钩这种机制?

【问题讨论】:

    标签: entity neo4j spring-data spring-data-neo4j


    【解决方案1】:

    从 Spring Data Neo4j 2.2 开始,我们可以使用 AuditingEventListener 来审计实体。 Spring Data 1.5 提供了@CreatedDate@CreatedBy@LastModifiedDate@LastModifiedBy 注释。您可以按如下方式使用它们:

    @NodeEntity
    public class Entity {
    
        @GraphId
        private Long id;
    
        @CreatedDate
        private Long date;
    
    }
    

    确保配置 AuditingEventListener:

    @Configuration("db")
    @EnableNeo4jRepositories(basePackages = { "your.package" })
    @EnableTransactionManagement
    public class DatabaseSpringConfiguration extends Neo4jConfiguration {
    
        @Bean(destroyMethod = "shutdown")
        public EmbeddedGraphDatabase graphDatabaseService() {
            return new EmbeddedGraphDatabase("data/neo4j.db");
        }
    
        @Bean
        public AuditingEventListener auditingEventListener() throws Exception {
            return new AuditingEventListener(new IsNewAwareAuditingHandler<Object>(isNewStrategyFactory()));
        }
    
    }
    

    【讨论】:

    • 并不是说当您通过 AspectJ 持久化时,这目前不适用于高级映射模式。见jira.springsource.org/browse/DATAGRAPH-335
    • 基于xml的配置怎么样?很难找到关于配置时间戳审计的教程/参考。
    • @kav1nsky 你是想说这个吗? &lt;neo4j:auditing /&gt;
    • @tstorms 我很想知道,为什么不给DateDateTime 的joda time 机会,为什么只给Long?参考stackoverflow.com/a/15461645/1660192
    • 对 spring data neo4j 3.3 执行此操作的正确方法是什么?
    【解决方案2】:

    Spring Data Neo4j (SDN) 在 2.1 版本中引入了lifecycle events 的概念。这也适用于级联实体。

    【讨论】:

    • 正是我需要的!我不知道这个功能。谢谢詹姆斯。
    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2011-12-23
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多