Skip to content

Clear exception message on recursive mapping of immutable objects. #2340

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
Andy2003 opened this issue Jul 26, 2021 · 4 comments
Closed

Clear exception message on recursive mapping of immutable objects. #2340

Andy2003 opened this issue Jul 26, 2021 · 4 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@Andy2003
Copy link
Contributor

I've created a Test case showing a StackOverflowError at https://github.com/Andy2003/spring-data-neo4j/tree/complex-test (Andy2003@d54e4be)

The error occurs when in a parent-child mapping the relation is modeled for both directions, like:

@org.springframework.data.neo4j.core.schema.Node
public abstract class BaseNodeEntity {
	@Relationship(type = "CHILD_OF", direction = OUTGOING)
	BaseNodeEntity childOf;
}

and

@org.springframework.data.neo4j.core.schema.Node
public class Node extends BaseNodeEntity {
	@Relationship(type = "CHILD_OF", direction = INCOMING)
	protected Set<BaseNodeEntity> childNodes;
}

and data like:

CREATE (root:Node:BaseNodeEntity{nodeId: 'root'})
CREATE (company:Node:BaseNodeEntity{nodeId: 'comp'})
CREATE (company)-[:CHILD_OF]->(root)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 26, 2021
@meistermeier meistermeier self-assigned this Jul 27, 2021
@meistermeier
Copy link
Collaborator

You are using the Lombok library with @AllArgsConstructor(onConstructor = @__(@PersistenceConstructor)) for the BaseNodeEntity. Combined with the manual all args @PersistenceConstructor in the Node you create a hen/egg scenario.
The mapping process wants to create the "root" Node but for this it needs the "comp" Node which depends again on the not-yet-complete-constructed "root" Node.
We will investigate if we could detect this incoming data-wise before running into the SO and log a warning or similar.

@Andy2003
Copy link
Contributor Author

Is it possible, that you inject the not jet fully populated object into the "child"-constructor, so you won't run into the cycle?
I used immutable Objects as the docs suggested it as best practice

@meistermeier
Copy link
Collaborator

No, we tried this more than once, but it is not possible to do this in any way.
For true immutability the root object for example has to be the same as the root object and the one you can reach by the parent/child object. Every approach failed because it is just not possible to assemble this.

@Andy2003
Copy link
Contributor Author

I found this on StackOverflow https://stackoverflow.com/a/4834584/1666829
I guess I have to care about this scenario in client code and use an approach similar to the one described in the post.

@meistermeier meistermeier changed the title Getting java.lang.StackOverflowError when using nodes where both ends of parent child hierarchy are mapped Clear exception message on recursive mapping of immutable objects. Jul 28, 2021
@meistermeier meistermeier added this to the 6.0.12 (2020.0.12) milestone Jul 28, 2021
@meistermeier meistermeier added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 28, 2021
meistermeier added a commit that referenced this issue Jul 28, 2021
Also improving the documentation around this topic.

Closes #2340
meistermeier added a commit that referenced this issue Jul 28, 2021
Also improving the documentation around this topic.

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

No branches or pull requests

3 participants