我在尝试从数据库中提取数据时遇到错误(数据存在于数据库中)。我可以在数据库中没有数据时执行操作。这是我的实体:
@Table(name = "tbl_task")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long taskId;
private String title;
private String description;
private Status status;
@ManyToOne
@JoinColumn(name = "user_id")
private User assignee;
@OneToMany(mappedBy = "task", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments;
以及:
@Entity
@Table(name = "tbl_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
private String name;
@Column(unique = true)
private String email;
private Active active;
private String password;
@OneToMany(mappedBy = "assignee", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Task> tasks;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments;
错误为:
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); nested exception is
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion
(StackOverflowError) (through reference chain:
com.tasksmanagement.entity.Task["assignee"]-
>com.tasksmanagement.entity.User["tasks"]-
>org.hibernate.collection.internal.PersistentBag[0]
方法如下:
public List<TaskResponse> getAllTasks() {
List<Task> tasks = taskRepository.findAll();
return
tasks.stream} }
你知道怎么解决吗?
谢谢
1条答案
按热度按时间j5fpnvbx1#
我在User类(List tasks字段)中添加了@JsonManagedReference,并在Task类(User assignee字段)中添加了@JsonBackReference,这样就解决了这个问题