使用Spring Data Neo4j进行审计

时间:2012-11-27 15:43:39

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

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

我提出的一个解决方案是编写一个AOP Aspect,它挂钩我的服务层的create方法。这适用于没有级联的实体,但级联的实体呢?这没有在我的服务层中明确传递,所以我的AOP类不会拦截它们。是否有像JPA中的实体侦听器这样的概念,或者我如何挂钩到这个机制?

2 个答案:

答案 0 :(得分:5)

从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()));
    }

}

答案 1 :(得分:1)

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