Skip to content

Track state of objects with assigned ids correctly. #2347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LGDOI opened this issue Aug 3, 2021 · 4 comments
Closed

Track state of objects with assigned ids correctly. #2347

LGDOI opened this issue Aug 3, 2021 · 4 comments
Assignees
Labels
type: bug A general bug

Comments

@LGDOI
Copy link

LGDOI commented Aug 3, 2021

I cannot tell if is a bug or a feature.

Given two node classes Application and Workflow. Application has outgoing 1-to-many relationship to Workflow.
Workflow has incoming relationship to Application.

The following code causes NPE during .saveAll call.

  @Test
  fun npe() {
    val app1 = Application()
    val wf1 = Workflow()
    val wf2 = Workflow()

    wf1.application = app1 // <===== Comment out to avoid NPE
    wf2.application = app1 // <===== Comment out to avoid NPE

    app1.workflows = listOf(wf1, wf2)
    applicationRepository.saveAll(listOf(app1))
  }

Note: if you comment out these two lines setting app1 to wf1 and wf2, NPE won't occur.
Note2: replace .saveAll(listOf(app1)) with .save(app1) solves the problem.
Note3: this example only shows one app1. but the same NPE happens even if the list size > 1

Stacktrace

java.lang.NullPointerException
	at org.springframework.data.neo4j.core.mapping.NestedRelationshipProcessingStateMachine$RelationshipDescriptionWithSourceId.equals(NestedRelationshipProcessingStateMachine.java:133)
	at java.base/java.util.HashMap.putVal(HashMap.java:630)
	at java.base/java.util.HashMap.put(HashMap.java:607)
	at java.base/java.util.HashSet.add(HashSet.java:220)
	at org.springframework.data.neo4j.core.mapping.NestedRelationshipProcessingStateMachine.markRelationshipAsProcessed(NestedRelationshipProcessingStateMachine.java:154)
	at org.springframework.data.neo4j.core.Neo4jTemplate.lambda$processNestedRelations$25(Neo4jTemplate.java:772)
	at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:387)
	at org.springframework.data.neo4j.core.Neo4jTemplate.processNestedRelations(Neo4jTemplate.java:658)
	at org.springframework.data.neo4j.core.Neo4jTemplate.lambda$processNestedRelations$25(Neo4jTemplate.java:801)
	at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:387)
	at org.springframework.data.neo4j.core.Neo4jTemplate.processNestedRelations(Neo4jTemplate.java:658)
	at org.springframework.data.neo4j.core.Neo4jTemplate.processRelations(Neo4jTemplate.java:649)
	at org.springframework.data.neo4j.core.Neo4jTemplate.lambda$saveAllImpl$14(Neo4jTemplate.java:482)
... more

Analysis

Neo4jTemplate.processNestedRelations contains a lambda there is a condition to set relatedInternalId on line 746-747.

if (stateMachine.hasProcessedValue(relatedValueToStore)) {
  relatedInternalId = stateMachine.getInternalId(relatedValueToStore);

When this condition is true, stateMachine.getInternalId(relatedValueToStore); return null.

Question

Should this condition return false if the stateMachine does not have an internal ID for relatedValueToStore?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 3, 2021
@michael-simons michael-simons self-assigned this Aug 9, 2021
@michael-simons
Copy link
Collaborator

Hi @LGDOI Are you able to share the classes for Application and Workflow? I am not able to reproduce this.

Also are you using 6.1.x or 6.0.x?

@michael-simons michael-simons added blocked: awaiting feedback and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 10, 2021
@michael-simons
Copy link
Collaborator

Never mind, I could figure out what I needed to reproduce it from your other ticket.

@michael-simons michael-simons changed the title NPE is thown by calling saveAll if incoming relationship object is set Track state of objects with assigned ids correctly. Aug 10, 2021
@michael-simons michael-simons added this to the 6.0.12 (2020.0.12) milestone Aug 10, 2021
michael-simons added a commit that referenced this issue Aug 10, 2021
This change makes the Cypher generator create a statement for persisting multiple instances with assigned ids that not only returns the assigned ID, but also the internally generated id. This id is needed for tracking the root objects state in the `NestedRelationshipProcessingStateMachine`.

Those ids must of course be retrieved. By doing so we currently loos the ability to debug log the number of nodes created.
While this removal of a log statement can be considered a breaking change, it is necessary.

To lighten that change: Storing multiple instances with internally generated did not debug log these informations at all.

This fixes #2347.
michael-simons added a commit that referenced this issue Aug 10, 2021
This change makes the Cypher generator create a statement for persisting multiple instances with assigned ids that not only returns the assigned ID, but also the internally generated id. This id is needed for tracking the root objects state in the `NestedRelationshipProcessingStateMachine`.

Those ids must of course be retrieved. By doing so we currently loos the ability to debug log the number of nodes created.
While this removal of a log statement can be considered a breaking change, it is necessary.

To lighten that change: Storing multiple instances with internally generated did not debug log these informations at all.

This fixes #2347.
@LGDOI
Copy link
Author

LGDOI commented Aug 10, 2021

Hi, @michael-simons,
Thank you for looking into this. So, this issue is related to other issue and will be fixed as part of other bug fix?

I believe I was on 6.1.3 when I saw this error (99.9%). But 0.1% chance I was on older version as I was switching b/w version to compare the API behavior.

Edit:

I just realized that you mentioned about 9ed7557. Thank you so much.

@michael-simons
Copy link
Collaborator

You're welcome and sorry for any inconvenience. Both reports have been very useful to us.

michael-simons added a commit that referenced this issue Aug 10, 2021
…ntities with assigned ids.

There is no need for an additional fix, the root cause had been addressed in #2347 already.

This closes #2346.
michael-simons added a commit that referenced this issue Aug 10, 2021
…ntities with assigned ids.

There is no need for an additional fix, the root cause had been addressed in #2347 already.

This closes #2346.
michael-simons added a commit that referenced this issue Aug 10, 2021
…ntities with assigned ids.

There is no need for an additional fix, the root cause had been addressed in #2347 already.

This closes #2346.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants