gpt4 book ai didi

Java Spring嵌套异常是java.lang.StackOverflowError

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:42 25 4
gpt4 key购买 nike

我有一些实体类并尝试从数据库获取所有数据,但是我遇到了嵌套异常问题。请参阅下面的详细信息: worker 实体:

   Worker{
@Id
private Long id;

@Transient
private List<WorkerDepartment> workerDepartments;

@Transient
private List<WorkerDepartmentPosition> workerDepartmentPositions;

@Transient
private List<WorkerAdjustmentBonus> workerAdjustmentBonus;
}

WorkerDepartment 实体

    WorkerDepartment{
@Id
private Long id;

@ManyToOne
@JsonBackReference(value = "worker")
@JoinColumn(name = "worker_id")
private worker worker;

}

WorkerDepartmentPosition 实体

    WorkerDepartmentPosition{
@Id
private Long id;

@Column(name = "worker_id")
private Long workerId;
}

worker 奖金

    WorkerBonus{
@Id
private Long id;

@ManyToOne
@JsonBackReference(value = "worker")
@JoinColumn(name = "worker_id")
private worker worker;

@ManyToOne
@JoinColumn(name = "payment_id")
private WorkerPayment employeePayment;

@Column(name = "bonus")
private Double bonus;
}

我尝试获取所有工作人员,响应数据为

{"status":"SUCCESS","data":[have full data],"message":"successfully done"}{"status":"ERROR","message":"Handler dispatch failed; nested exception is java.lang.StackOverflowError"}

请给我建议。

最佳答案

该问题可能来自于例如以下内容的交叉引用: WorkerWorkerDepartment。当 Jackson 尝试反序列化您的对象时,它会遍历对象图,一旦访问 WorkerDepartment 属性,它就会返回到 Worker 并一遍又一遍地执行此循环。

由于您已经使用了@JsonBackReference(value = "worker"),您几乎已经解决了问题,但是您需要在另一侧使用@JsonManagedReference:

public class Worker{
@Id
private Long id;

@JsonManagedReference
private List<WorkerDepartment> workerDepartments;

@JsonManagedReference
private List<WorkerDepartmentPosition> workerDepartmentPositions;

@JsonManagedReference
private List<WorkerAdjustmentBonus> workerAdjustmentBonus;
}

Baeldung 还提供 a good tutorial对此进行进一步阅读。

关于Java Spring嵌套异常是java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com