0

I am trying to update a Person entity in Neo4J Community edition 3.0.3 using SDN (spring-data-neo4j 4.1.2.RELEASE). I am seeing a kind of behavior while updating an entity.

  • I created a 'Person' entity of the name "person" and saved the same in the database (line 8).
  • Changed a property (fullName) of the saved entity but did not update that in the database (line 10).
  • Retrieved the same person from the database but by using a findBy method into another variable named "person2" line(12).
  • The changes made in variable "person" (in line 10) are lost.
  • Both person and person2 variables now have the same property values.

    1.Person person = new Person();
    2. person.setUuid(UUID.randomUUID().toString());
    3. person.setFullName("P1");
    4. person.setEmail("PersonP1@gmail.com");
    5. person.setUsername("PersonP1@gmail.com");
    6. person.setPhone("123456789");
    7. person.setDob(new Date());
    8. personService.create(person);
    
    9. System.out.println(person);
    //Person{id=27, username='PersonP1@gmail.com', fullName='P1', email='PersonP1@gmail.com'}
    10. person.setFullName("P2");
    11. System.out.println(person);
    //Person{id=27, username='PersonP1@gmail.com', fullName='P2', email='PersonP1@gmail.com'}
    
    12.Person person2 = personService.findByEmail("PersonP1@gmail.com");
    13. System.out.println(person2);
    //Person{id=27, username='PersonP1@gmail.com', fullName='P1', email='PersonP1@gmail.com'}
    14. System.out.println(person);
    //Person{id=27, username='PersonP1@gmail.com', fullName='P1', email='PersonP1@gmail.com'}
    

Is this the default behavior of Neo4J SDN ?

Given below are the pom entries as well as the configuration used for Neo4J as advised in the comment

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <!-- <version>4.1.2.RELEASE</version> -->
    </dependency>

    <dependency> 
        <groupId>org.neo4j</groupId> 
        <artifactId>neo4j-ogm-core</artifactId> 
        <version>2.0.4</version> 
    </dependency>


public class MyNeo4jConfiguration extends Neo4jConfiguration {
@Bean
public org.neo4j.ogm.config.Configuration getConfiguration() {
    org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
    config
       .driverConfiguration()
       .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver")
       .setCredentials("neo4j", "admin")
       .setURI("http://localhost:7474");
   return config;
}

@Bean
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), "au.threeevolutions.bezzur.domain"  );
}
}
Soumya
  • 1,833
  • 5
  • 34
  • 45

1 Answers1

1

This behaviour has been fixed in the latest version of Neo4j OGM- 2.0.4 If you reload an entity that the session is already tracking, the entity properties will not be overwritten i.e. the properties in the session cache are returned, preserving your unpersisted modifications. Note however, that relationships and new nodes may be added to the subgraph in the session if these are pulled in by loading related nodes for example.

Luanne
  • 19,145
  • 1
  • 39
  • 51
  • I am using spring-data-neo4j 4.1.2.RELEASE. So do I need to change to neo4j-ogm ? And will it incur a lot of changes in the code if I am switching ? Its a spring-boot based project. I have only worked on the spring-data till date (as you might have already noticed from my earlier posts) – Soumya Jul 23 '16 at 05:01
  • No need to drop down to Neo4j OGM. Just include this dependency in your pom: org.neo4j neo4j-ogm-core 2.0.4 and one for your driver see http://neo4j.com/docs/ogm-manual/current/#_dependencies_for_the_neo4j_ogm – Luanne Jul 25 '16 at 01:16
  • I have made changes accordingly. The modifications have been provided in the question itself. Now the "fullName" property of both the variables "person" and "person2" have their values as "P2". It seems that if the same nodes are fetched from the database, the local changes in those nodes are reflected in the fetched ones. For example, I changed the value of fullName to "P2" for variable "person1". Now when I fetched the details of the same person using the findByEmail method into variable "person2", the fullName of this variable is also "P2" and not "P1" although I have not persisted person1 – Soumya Jul 25 '16 at 10:56
  • Yes Luanne, the problem I had has been resolved. If there are any local changes to a variable representing a fetched node, the changes are persistent whenever that node is fetched again. Thanks a lot. – Soumya Aug 04 '16 at 11:19
  • Thanks for confirming, please accept the answer if it helped! – Luanne Aug 04 '16 at 11:54