使用Spring Data Neo4j进行审核 [英] Audits with Spring Data Neo4j

查看:128
本文介绍了使用Spring Data Neo4j进行审核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一个使用Spring Data Neo4j的项目.每当创建NodeEntity时,我都想创建一个包含创建日期和用户的引用的审计NodeEntity.

I'm currently working on a project that makes use of Spring Data Neo4j. Whenever a NodeEntity is created, I would like to create a referenced Audit NodeEntity that contains the creation date and user.

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

A solution that I've come up with, is to write an AOP Aspect which hooks in on the create method of my service layer. This works fine for entities that aren't cascaded, but what about the cascaded ones? That are not explicitly passed in my service layer so my AOP class will not intercept them. Is there a concept like entity listeners in JPA, or how can I hook into this mechanism?

推荐答案

从Spring Data Neo4j 2.2开始,我们可以使用AuditingEventListener进行实体审计. Spring Data 1.5提供了 @CreatedDate @ LastModifiedDate

Since Spring Data Neo4j 2.2, we can use the AuditingEventListener for the auditing of entities. Spring Data 1.5 offers the @CreatedDate, @CreatedBy, @LastModifiedDate and @LastModifiedBy annotations. You can use them as follows:

@NodeEntity
public class Entity {

    @GraphId
    private Long id;

    @CreatedDate
    private Long date;

}

确保配置AuditingEventListener:

Make sure to configure the 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()));
    }

}

这篇关于使用Spring Data Neo4j进行审核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆