我目前正在开发一个使用 Spring Data Neo4j 的项目。每当创建 NodeEntity 时,我想创建一个引用的 Audit NodeEntity,其中包含创建日期和用户。
我提出的一个解决方案是编写一个 AOP 方面,它挂接在我的服务层的 create 方法上。这对于未级联的实体来说效果很好,但是级联的实体又如何呢?它们没有在我的服务层中显式传递,因此我的 AOP 类不会拦截它们。 JPA 中是否有实体侦听器之类的概念,或者我如何挂钩此机制?
从 Spring Data Neo4j 2.2 开始,我们可以使用 AuditingEventListener 来审计实体。 Spring Data 1.5 提供了@创建日期, @由...制作, @最后修改日期 and @最后修改者注释。您可以按如下方式使用它们:
@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()));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)