[英]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;
}
问题当我尝试获取用户时,我返回 StackOverflowException。
我怎样才能解决这个问题?
我试图删除课程 Class 上的关系并且它有效。 但是在 Course Object 里 Response 里是同一个用户。 在用户中是课程等。
Spring 数据 Neo4j 检测到诸如User
<> Course
之类的循环,并发出由其接收到的数据驱动的级联MATCH
查询。 如果因为 a) 没有任何东西或 b) 已经访问过而没有任何东西要加载,它就完成了。 没有已知的情况会遇到StackOverflow
。
我的假设是您正在运行 web 应用程序,output 格式为 JSON。因此问题出在 JSON 映射器中,因为它遵循每个链接属性(直到您遇到溢出)。 为避免这种情况,您应该将@JsonManagedReference
、 @JsonIdentityInfo
或@JsonIgnore
添加到对您的用例有意义的实体中。
我们将此提示作为另一个映射库的一部分,但同样适用https://neo4j.com/docs/ogm-manual/current/reference/#_a_note_on_json_serialization
我的同事 Michael 撰写了更深入的观点,也采用了更具差异化的方法。 https://gist.github.com/michael-simons/04807990778a10a233e55bb7a2f6d6bc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.