I am working on a Spring Boot application with a Neo4j backend, and I need to implement automatic tracking for entity auditing. Specifically, I would like to track createdDate,updatedDate , createdBy and updatedBy
I’ve heard of Neo4j Auditing, but I’m unsure about the complete steps to achieve this. Can someone please guide me on how to achieve
The auditing feature is available in Spring Data Neo4j and can be used as shown here in the reference documentation. FAQ :: Spring Data Neo4j
It is really as simple as described. First you activate the auditing mechanism via @EnableNeo4jAuditing
. Even without the beans that are mentioned in the example of the documentation, you would get the dates set.
For the principal, you would either have to define a bean like shown in the example or an AuditorAware<T>
implementation as shown here Auditing :: Spring Data Neo4j
A side-note on this: You have to use an identifier based on a id generator (like the built-in UUIDGenerator
) for this to work 100%. If you have a fixed property for the id that you handle manually, SDN cannot determine if this entity is new or updated on save and won't update the modified fields, because -for defensive save reasons- it assumes the object in those cases always as new.
@SpringBootApplication
@EnableScheduling
@EnableNeo4jAuditing
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Node
public class MyCard {
@Id
private String myId;
@CreatedBy
private User createdBy;
@CreatedDate
private LocalDateTime createdDate;
}
class User implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
return Optional.of("WIFIFIFIFI");
}
}
I really dont undertsand
what further should I do
What is your exact problem? Does it not work at all or won't "just" the @Modified...
get updated?
@Id
private String myId;
This is exactly the style of identifier to avoid (esp. when dealing with auditing).
It's not working at all. I have my own ID
I just don't want to use the auto-generated values. I have my own ID as a string.
Let's say a car is a node. I don't want the autogenerated value as the ID; I will use my own data for the ID. However, I need to track who created the entry, when it was created, who updated it, and when it was updated, i.e., createdAt, createdBy, updatedAt.
The problem with the assigned ids is exactly what I was referring to in my comments above. For Spring Data Neo4j an entity with an assigned (self-managed) id is always new unless it has also versioning/optimistic locking enabled (Metadata-based Mapping :: Spring Data Neo4j).
Here is an example for the use of generated UUIDs with auditing (neo4j-issues-examples/discourse-71553 at 4e2fb2d3e079de206624091f90fab1cc143a7127 · meistermeier/neo4j-issues-examples · GitHub) .
While creating it, I figured out that the resulting problem with provided ids and without versioning is not that the modify dates won't get written but it will always be createdAt/By having the same value as the last modify fields.
So either add optimistic locking and stick with the assigned ids..
..or use generated, synthetic ids.