Java 将表联接添加到现有项目会导致无限递归
我在这个项目上工作: 在这里,您可以找到实体等的所有源。我试图做的是在Java 将表联接添加到现有项目会导致无限递归,java,mysql,spring,hibernate,Java,Mysql,Spring,Hibernate,我在这个项目上工作: 在这里,您可以找到实体等的所有源。我试图做的是在TODO和USER之间添加一个连接。我希望这是一个多对一的关系,因为一个用户可以有多个TODO,但TODO只能有一个所有者 我在MySQL数据库中创建了一个连接表,其中包含外键等 然后我添加了以下注释: 这会在应用程序启动时引发致命错误: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [R
TODO
和USER
之间添加一个连接。我希望这是一个多对一的关系,因为一个用户可以有多个TODO,但TODO只能有一个所有者
我在MySQL数据库中创建了一个连接表,其中包含外键等
然后我添加了以下注释:
这会在应用程序启动时引发致命错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError) (through reference chain: sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]->org.hibernate.collection.internal.PersistentSet[0]->sample.todosapp.spring.domain.Todo["user"]->sample.todosapp.spring.domain.User["todos"]
我完全迷路了。我搜索了所有相关的问题,并尝试了所有可能的注释变体,但仍然没有luch。我到底做错了什么?您应该在一个属性中使用mappedBy,而不是在两个属性中定义关系。从HashSet Todo中删除注释@JoinColumn,并将
@OneToMany(cascade=CascadeType.ALL)
更改为@OneToMany(mappedBy=“user”,cascade=CascadeType.ALL)
我遇到了同样的问题,尝试了不同的方法。如果您在代码中同时需要关系(@OneToMany
和@ManyToOne
),您应该怎么做
因此,您应该使用@JsonIgnore
注释来打破循环。问题解决了。就像书中建议的那样
我的答案晚了,但希望能帮助别人 我已经将fetch类型更改为.LAZY,现在它可以编译并运行,但问题仍然存在