Skip to content

Discrepency between Neo4jTemplate saveAs and saveAllAs #2407

Closed

Description

@jposthauer

It appears that saveAllAs saves all properties regardless of whether those properties are in the projection passed as the Class<R> resultType parameter, while saveAs only saves the properties that are in the projection.

On the other hand, relationships seem to behave the same for both, if the relationship isn't included in the projection then it is not saved.

The expected behavior is that properties behave just like relationships for both saveAs and saveAllAs.

Activity

  1. jposthauer commented on Oct 18, 2021

    @jposthauer
    Author

    Spring Data Neo4j: 6.1.5

  2. michael-simons commented on Oct 19, 2021

    @michael-simons
    Collaborator

    Hi @jposthauer What do you mean with by "only saves the properties that are in the projection." ? Non null values or actual present fields regardless of their value? I can't reproduce this in our test:

    @Test
    void saveAllAsWithClosedProjectionShouldWork() {
    // Using a query on purpose so that the address is null
    Person p1 = neo4jTemplate.findOne("MATCH (p:Person {lastName: $lastName}) RETURN p",
    Collections.singletonMap("lastName", "Siemons"), Person.class).get();
    Person p2 = neo4jTemplate.findOne("MATCH (p:Person {lastName: $lastName}) RETURN p",
    Collections.singletonMap("lastName", "Schnitzel"), Person.class).get();
    p1.setFirstName("Micha");
    p1.setLastName("Simons");
    p2.setFirstName("Helga");
    p2.setLastName("Schneider");
    List<ClosedProjection> openProjection = neo4jTemplate
    .saveAllAs(Arrays.asList(p1, p2), ClosedProjection.class);
    assertThat(openProjection).extracting(ClosedProjection::getLastName)
    .containsExactlyInAnyOrder("Simons", "Schneider");
    List<Person> people = neo4jTemplate.findAllById(Arrays.asList(p1.getId(), p2.getId()), Person.class);
    assertThat(people).extracting(Person::getFirstName).containsExactlyInAnyOrder("Michael", "Helge");
    assertThat(people).extracting(Person::getLastName).containsExactlyInAnyOrder("Simons", "Schneider");
    assertThat(people).allMatch(p -> p.getAddress() != null);
    }

    This saves only the last names (fields that are included in the projection)

  3. jposthauer commented on Oct 19, 2021

    @jposthauer
    Author

    Below is the general setup we are trying to use. Once the last line is called it's still saving both first and last name instead of just the last name, while the relationship is not saved (expected).

    @Node
    public class Person {
      @Property
      private String firstName;
      
      @Property
      private String lastName;
      
      @Relationship("HAS_CHILD")
      private List<Person> children;
    }
    
    
    public interface PersonWithOnlyLastName {
      String getLastName();
    }
    
    
    Person person = new Person("John","Doe")
    Person child = new Person("Jane", "Doe"
    person.children = listOf(child)
    Neo4jTemplate.saveAllAs(listOf(person, child), PersonWithOnlyLastName.class)
    
  4. michael-simons commented on Oct 19, 2021

    @michael-simons
    Collaborator

    But you are using saveAs, not saveAllAs

    There are tests for that exact scenario.

    Note: The projection won't be applied to the relationship (the children won't be project…)

    Can it be that your child elements have a back reference to the parent?

  5. jposthauer commented on Oct 19, 2021

    @jposthauer
    Author

    Apologies, I fixed my example to use saveAllAs(). To clarify we don't get this issue on saveAs, only saveAllAs.
    Also we see this issue even with entities that only have properties and no relationships.

  6. added a commit that references this issue on Oct 19, 2021
  7. michael-simons commented on Oct 19, 2021

    @michael-simons
    Collaborator

    No worries, all good.

    But sorry from my side, I need more input. I cannot reproduce it, see the added tests here: a7e7acf

  8. jposthauer commented on Oct 19, 2021

    @jposthauer
    Author

    We will do more investigation on our end to see what's causing it with our setup.

  9. jposthauer commented on Oct 20, 2021

    @jposthauer
    Author

    I was able to replicate the issue with a simple app: https://github.com/jposthauer/SimpleApp/blob/master/src/test/java/com/example/ApplicationTests.java

    The first test passes but the second does not.

  10. michael-simons commented on Oct 21, 2021

    @michael-simons
    Collaborator

    Thank you, I can reproduce this now. Expect a fix soon.

  11. added a commit that references this issue on Oct 21, 2021
  12. michael-simons commented on Oct 21, 2021

    @michael-simons
    Collaborator

    Thanks again for reporting this @jposthauer and providing the reproducer, much appreciated and valuable.

  13. added a commit that references this issue on Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

type: bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions