Search code examples
neo4jspring-data-neo4j-4neo4j-ogm

Updating properties of Neo4J Entity using SDN


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"  );
}
}

Solution

  • 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.