Spring Data Neo4J 4.0.0: BeforeSaveEvent not firing?
I'm trying to grab BeforeSaveEvent while setting up Neo4J in Spring so that I can call a method beforeSave()
on the class that is being saved. Unfortunately, it looks like it is not registering as a listener as my print instructions are not being executed. In the meantime, there is no need to know about it. β
Ideas appreciated.
@Configuration
@EnableNeo4jRepositories(basePackages = "com.noxgroup.nitro")
@EnableTransactionManagement
public class NitroNeo4jConfiguration extends Neo4jConfiguration {
@Bean
public Neo4jServer neo4jServer () {
System.setProperty("username", "neo4j");
System.setProperty("password", "*************");
return new RemoteServer("http://localhost:7474");
}
@Bean
public SessionFactory getSessionFactory() {
return new SessionFactory("com.noxgroup.nitro.domain");
}
@Bean
ApplicationListener<BeforeSaveEvent> beforeSaveEventApplicationListener() {
return new ApplicationListener<BeforeSaveEvent>() {
@Override
public void onApplicationEvent(BeforeSaveEvent event) {
System.out.println("Listening to event");
Object entity = event.getEntity();
if (entity instanceof NitroNode) {
((NitroNode)entity).beforeSave();
} else {
System.out.println("Not picking it up");
}
}
};
}
}
+3
source to share
1 answer
These events are triggered using the Neo4jTemplate (see http://docs.spring.io/spring-data/neo4j/docs/4.0.0.M1/reference/html/#_data_manipulation_events_formerly_lifecycle_events ), so you will have to use to trigger save ...
In your configuration NitroNeo4jConfiguration
include
@Bean
public Neo4jOperations getNeo4jTemplate() throws Exception {
return new Neo4jTemplate(getSession());
}
and in your application
@Autowired
private Neo4jOperations neo4jTemplate;
which is then used to save
neo4jTemplate.save(person);
+2
source to share