Spring Boot - SDN 5 - Neo4j Auditing @CreatedDate doesn't work

858 Views Asked by At

I'm having some issues getting the @CreatedDate among other auditing functionality from working at all. I believe I've followed the guidance provided in the Reference Document but I believe something may be missing. I'm currently using Spring Boot 2.0.0.M7 which uses SDN 5.0.2. I'm trying a very simple example but regardless of whether I use Long, LocalDate, or even Date I never get it populated when I view the data in Neo4j browser or when I try loading saved nodes back into the POJO.

Here's a snippet of my object:

@NodeEntity
public class Person {

    @Id @GeneratedValue private Long id;

    @CreatedBy
    private String user;

    @CreatedDate
    public Long createdDate;

    private String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

I have a very simple repository that that only has a findByName method, and the main application doesn't do too much either, but it looks like this:

@SpringBootApplication
@EnableNeo4jRepositories
public class Application {

    private final static Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    CommandLineRunner demo(PersonRepository personRepository) {
        return args -> {

            personRepository.deleteAll();
            Person bob = new Person("Bob");
            personRepository.save(bob);
            Person bob2 = personRepository.findByName(bob.getName());

            log.info("Name: " + bob2.getName() + " Created Date: " + bob2.createdDate);
        };
    }
}

The output from the logs show that createdDate is still null:

Name: Bob  Created Date = : null 

I've even created a class that implemented AuditorAware, but the application never triggered that code. I think I'm missing some annotation and tried @EnableNeo4jAuditing, but that causes The dependencies of some of the beans in the application context form a cycle:

I can't seem to find what I'm missing. I've seen in SDN 4 some people have set up an application listener, but that was prior to auditing support. I feel like I've done my research, but I have hit a wall.

1

There are 1 best solutions below

3
meistermeier On

You were right: the annotation @EnableNeo4jAuditing is required to enable the auditing feature. But this result in an error at the moment.

When the SpringBoot Neo4j auto configuration gets applied it will create a SessionFactory and requires all EventListeners to be instantiated before to register them all in the SessionFactory. On the other side the Neo4jAuditingEventListener requires the SessionFactory to register itself in it. The outcome is the cyclic dependency error while starting the application.

This is/was clearly a bug in SpringData Neo4j 5.0 (up to version 5.0.3). It will be fixed in the next SpringData Neo4j version 5.1.0 M1 and also in the next service release for SpringData Neo4j 5.0 (SR 4).

As mentioned in the comments, here is the link to the issue to keep track of this.