[英]getting infinite recursion with JPA hibernate by @OneToMany and @ManyToOne
[英]Infinite recursion reference with JPA OneToMany, ManyToOne Relationship
我在下面做了一些域。
@Entity
public class Conference {
...
@OneToMany(
targetEntity = ProgramDate.class,
mappedBy = "conference",
cascade = CascadeType.REMOVE,
fetch = FetchType.EAGER
)
@JsonBackReference
private List<ProgramDate> programDateList;
}
@Entity
public class Program {
...
@ManyToOne
@JoinColumn(name = "program_date_id")
@JsonBackReference
private ProgramDate date;
@ManyToOne
@JoinColumn(name = "room_id")
private Room room;
...
}
@Entity
public class ProgramDate {
...
@OneToMany(
targetEntity = Program.class,
mappedBy = "date",
fetch = FetchType.EAGER
)
@JsonBackReference
private List<Program> programList;
@ManyToOne
@JoinColumn(name = "conference_id")
private Conference conference;
}
@Entity
public class Room {
...
@OneToMany(
targetEntity = Program.class,
mappedBy = "room",
fetch = FetchType.EAGER
)
@JsonBackReference
private List<Program> programList;
}
我像下面的代码一样制作了freemarker。
<#list conference.programDateList as date>
...
</#list>
我遇到了一个问题,即 JPA OneToMany、ManyToOne 关系的无限递归引用。 我尝试添加@JsonBackReference
,但它只解决了 json 递归问题。
使用 json 忽略注释:
@OneToMany(
targetEntity = Program.class,
mappedBy = "date",
fetch = FetchType.EAGER
)
@JsonIgnore
private List<Program> programList;
为什么你在 Conference 中包含 Room & Programdate,然后在 Program 中添加一个 Conference 沿着 Room 和 Programdate 应该已经包含在 Conference 中? 然后在 ProgramDate 你有另一个参考...会议和程序列表...
基本上你不应该尝试用一些花哨的注释来“破解”这些循环,但你应该处理你的数据模型。 虽然 Conference 看起来不错,但 Program 只能是会议列表,并且 Programdate 应该是...日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.