1
votes

I'm getting org.neo4j.ogm.metadata.MappingException: Infinite recursion (StackOverflowError) while performing a repository query. The project was ported from SDN 3.

Sample domain models:

@NodeEntity
public class Person {
    ...
    @Relationship(type = "FRIENDSHIP")
    private Set<Friendship> friendships = new HashSet<Friendship>();
    ...
}

@RelationshipEntity
public class Friendship {
    ...
    @StartNode private Person person1;
    @EndNode private Person person2;
    Date since;
    ...
}

The exception is thrown when the following query is run:

@Query("MATCH (person1 {id: {0}.id})-[rel:FRIENDSHIP]->(person2 {id: {1}.id}) "
        + "return rel")
Friendship getFriendship(Person person1, Person person2);

Exception:

org.neo4j.ogm.metadata.MappingException: Infinite recursion (StackOverflowError) (through reference chain: com.example.domain.Friendship["person1StartNode"]->com.example.domain.Person["friendships"]->java.util.HashSet[0]->com.example.domain.Friendship["niperson1StartNode"]->com.example.domain.Person["friendships"]......

I thought this might be to do with @StartNode and @EndNode being the same type. But I got the same exception when @EndNode was of some other type.

Working with snapshots.

2

2 Answers

2
votes

I had the same error, but after some findings, I noticed that it was due to Jackson serializing the model object into JSON, getting into infinite recursion.

Solution was to either add a @JsonIgnore on the members causing problem, or just populate a DTO from the Model and return it in the API layer instead.

I took the first choice because I'm working on a prototype and needed to iterate quickly, but the second option with some BeanUtils magic may avoid this kind of JsonIgnore stuff.

1
votes

Could you please change the query to

@Query("MATCH (person1 {id: {0}})-[rel:FRIENDSHIP]->(person2 {id: {1}}) "
        + "return rel")
Friendship getFriendship(long person1, long person2);

(or the correct datatype of id)

Parameters that are entities themselves are not supported.

Having said that, the exception isn't helpful at all. Opened https://jira.spring.io/browse/DATAGRAPH-694