Warm tip: This article is reproduced from stackoverflow.com, please click
java neo4j neo4j-ogm spring spring-boot

Neo4j Spring ogm causes stackoverflow for followers-followings concept

发布于 2020-04-23 18:37:22

Trying to implement simple social network concept: followers + followings. All are just Accounts related to each other. Using Neo4j and Spring boot.

'org.springframework.boot' version '2.2.4.RELEASE'
'org.springframework.boot:spring-boot-starter-data-neo4j'

Account.class

@NodeEntity
public class Account {

  @Id
  private String pk;

  @Relationship(type = "FOLLOWS", direction = Relationship.INCOMING)
  private Set<Account> followers;

  @Relationship(type = "FOLLOWS", direction = Relationship.OUTGOING)
  private Set<Account> followings;
}

Using standart spring CrudRepository (or Neo4JRepository, no difference) i constantly get stackoverflows working with accounts. Simplest situation where A follows B and B follows A will cause a SOF for findById(). I understand that it has smth. to do with fetching depth, but it defaults to 1, which means that i should get followers and followings of account but theirs relations should be empty. The relations work correctly in terms of adding: i can connect accounts from any end by adding to appropriate Set and graph turns out exactly as i intend, but fetching is not working..

Main question: What am i doing and understanding wrong, and what can be done to implement this kind of relations correctly?

I tried using session.load() with depth 0, and no SOFs, of course, but that is not exactly what i need. Default depth 1 should provide exactly what i need, but i guess i misunderstand the concept?

And extra: is there a way to configure default depth for entire application? Without using session manually, thus reimplementing all basic operations..

Questioner
Emptyfruit
Viewed
14
Emptyfruit 2020-02-13 20:14

Finally, got it. The real problem was not in fetching, but inside internal call for equals() and hashcode() somewhere during fetch. Since i use lombok with @Data, all properties paricipated in those methods, causing SOF.

Adding @EqualsAndHashCode(exclude={"followers", "followings"}) to class's annotations fixed the issue and depth=1 works now.