3

In my Spring Data Neo4j 4 project - BeforeSaveEvent class is deprecated.

Also, previously I used a following code in order to setup created/updated date for my entities:

@EventListener
public void handleBeforeSaveEvent(BeforeSaveEvent event) {
    Object entity = event.getEntity();
    if (entity instanceof BaseEntity) {
        BaseEntity baseEntity = (BaseEntity) entity;
        if (baseEntity.getCreateDate() == null) {
            baseEntity.setCreateDate(new Date());
        } else {
            baseEntity.setUpdateDate(new Date());
        }
    }
}

but right now this listener is not invoked.

Is there any replacement for this logic in Neo4j 4 ? I'll really appreciate an example. Thanks

UPDATED

The configuration described below is working but some of my tests are fail because of NULL dates on a previously saved entities.. something is still wrong..

After clarification found a reason of this issue and waiting for this bugfix Modifications during a onPreSave() event do not persist to the database

@Configuration
@EnableExperimentalNeo4jRepositories(basePackages = "com.example")
@EnableTransactionManagement
public class Neo4jTestConfig {

    @Bean
    public Neo4jTransactionManager transactionManager() throws Exception {
        return new Neo4jTransactionManager(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {
        return new SessionFactory("com.example") {

            @Override
            public Session openSession() {
                Session session = super.openSession();

                session.register(new EventListenerAdapter() {

                    @Override
                    public void onPreSave(Event event) {
                        Object eventObject = event.getObject();
                        if(eventObject instanceof BaseEntity) {
                            BaseEntity baseEntity = (BaseEntity) eventObject;
                            if (baseEntity.getCreateDate() == null) {
                                baseEntity.setCreateDate(new Date());
                            } else {
                                baseEntity.setUpdateDate(new Date());
                            }
                        }
                    }

                });

                return session;
            }

        };
    }

}
Luanne
  • 19,145
  • 1
  • 39
  • 51
alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • 1
    Facing with the same problem...I found [this question](http://stackoverflow.com/questions/30604863/spring-data-neo4j-4-0-0-beforesaveevent-not-firing) here on SO and this is what we've done at the moment (always update graph entities through Neo4jTemplate instead of GraphRepository). If you use the template, the before save event will be fired. I know it's not an ideal solution, but maybe can help you until you find a better choice. – troig Aug 04 '16 at 07:51
  • 1
    @troig, thanks ! I'm using a mix of Session with SDN Repositories.. so looks like this is not an option especially in case of repository.. – alexanoid Aug 04 '16 at 17:06
  • Yes I know it's not a really good option...There is an open issue in [sdn jira](https://jira.spring.io/browse/DATAGRAPH-710), maybe you could have a look there and get some feeback. – troig Aug 04 '16 at 17:44

1 Answers1

3

You must be using Spring Data Neo4j (SDN) 4.2.0.M1. This has not been officially released yet but you are free to test it while it is undergoing the Spring Data Release process.

The event code in SDN has been deprecated in favour of a variety of mechanisms. Number one is that Spring Data now support Transaction aware event listeners. You can check out how to implement those here. Number two is that you can now autowire the Neo4j OGM Session into your application and take advantage of it's event capabilities (see the register() method).

Finally you are able to marry the two concepts up together and get OGM generated events fired through Spring!

Documentation will come as we continue the release but for now feel free to play with it yourself.

digx1
  • 1,100
  • 5
  • 9
  • I still can't find a working solution.. my tests fail with a configuration in my question. For a some reasons dates are not persisted correctly with this approach. – alexanoid Aug 03 '16 at 19:18