Spring 数据 Neo4j 中的双向关系

Bidirectional Relationships in Spring Data Neo4j

我正在尝试创建一个系统来跟踪我学校的一些课程。 有多名学生和多名教师的课程。 学生和教师表示为用户。

@Node
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {

 @Id
 @GeneratedValue(generatorClass = GeneratedValue.UUIDGenerator.class)
 private UUID id;

 private String name;
 private String password;
 private UserType type;

 @Relationship(type = "ATTENDS", direction = OUTGOING)
 private School school;

 @Relationship(type = "IN_COURSE", direction = OUTGOING)
 private Collection<Course> courses;

 @Relationship(type = "TEACHES", direction = OUTGOING)
 private Collection<Course> teaches;

 public enum UserType {
    STUDENT,
    TEACHER;
 }

}

@Node
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Course {

 @Id
 @GeneratedValue(generatorClass = GeneratedValue.UUIDGenerator.class)
 private UUID id;

 private String name;

 @Relationship(value = "TEACHES", direction = INCOMING)
 private User teacher;
}

问题 当我尝试获取用户时,我返回了 WhosebugException。

我该如何解决这个问题?

我试图删除课程 Class 上的关系,但它奏效了。但在 Response 所在的 Course Object 中是同一个用户。在用户中是课程等等。

Spring Data Neo4j 检测到类似 User<>Course 的循环,并发出由其接收的数据驱动的级联 MATCH 查询。 如果因为 a) 没有任何东西或 b) 已经访问过而没有任何东西要加载,它就完成了。 没有已知的情况可以 运行 变成 Whosebug

我的假设是您正在 运行 网络应用程序并且输出格式是 JSON。 所以问题出在 JSON 映射器中,因为它跟随每个链接的 属性 (直到你 运行 进入溢出)。 为避免这种情况,您应该将 @JsonManagedReference@JsonIdentityInfo@JsonIgnore 添加到对您的 use-case.

有意义的实体中

我们将此提示作为另一个映射库的一部分,但同样适用 https://neo4j.com/docs/ogm-manual/current/reference/#_a_note_on_json_serialization

我的同事 Michael 编写了一个更 in-depth 的观点,也采用了更具差异化的方法。 https://gist.github.com/michael-simons/04807990778a10a233e55bb7a2f6d6bc