将表连接添加到现有项目导致无限递归 [英] Adding table joins to existing project causes infinite recursion

查看:166
本文介绍了将表连接添加到现有项目导致无限递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个项目上工作:





这就是在应用程序启动时引发致命错误:

  Servlet.service()对于path []中的servlet [dispatcherServlet]引发异常[Request processing failed;嵌套异常是org.springframework.http.converter.HttpMessageNotWritableException:无法写入JSON:无限递归(StackOverflowError)(通过引用链:sample.todosapp.spring.domain.User [todos]  - > org.hibernate.collection .internal.PersistentSet [0]  - > sample.todosapp.spring.domain.Todo [ 用户]  - > sample.todosapp.spring.domain.User [ 待办事项]  - > org.hibernate.collection。 internal.PersistentSet [0]  - > sample.todosapp.spring.domain.Todo [ 用户]  - > sample.todosapp.spring.domain.User [ 待办事项]  - > org.hibernate.collection.internal .PersistentSet [0]  - > sample.todosapp.spring.domain.Todo [ 用户]  - > sample.todosapp.spring.domain.User [ 待办事项]  - > 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)


I work on this project: https://github.com/s1ddok/todosapp

Here you can find all sources for entity etc. What I'm trying to do is to add a connection between TODO and USER. I want that to be a many-to-one relation since one user can have multiple todos, but todos can only have one owner.

I've created a join table in my MySQL database, with foreign keys etc.

Then I added following annotations:

And that is raising fatal error on Application start:

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"]

......

I'm completely lost. I search all related questions and tried all possible annotation variants, but still have no luch. What exactly I did wrong?

解决方案

You should use mappedBy in one of your attributes and not define the relationship in both of them. Remove the annotation @JoinColumn from the HashSet Todo and change @OneToMany(cascade = CascadeType.ALL) to @OneToMany(mappedBy = "user" , cascade = CascadeType.ALL)

这篇关于将表连接添加到现有项目导致无限递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆