![](/img/trans.png)
[英]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.