我可能误解了JPA的一些基础知识。我在返回的对象上得到了一种循环引用......。
我有一个问题列表,每个问题都有一个响应列表.但随着JPA映射我做了,每个响应也有一个问题。请看这里。
@Entity
public class Question implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// A question has several responses
@OneToMany(mappedBy = "question", fetch = FetchType.EAGER)
private List<Reponse> reponses;
@Entity
public class Reponse implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
// This is here to specify the join column key "question_id"
@ManyToOne
@JoinColumn(name = "question_id")
private Question question;
正如你所看到的,我有一个问题[0],它有一个响应列表,但每个问题都有一个问题,它也有一个响应列表,等等... ..:
我怎样才能指定连接列键而不需要把整个对象和他的所有子对象等等?
非常感谢您的帮助!
Joss
在你所做的配置中,流按以下顺序检索数据。
Question
对象Response
对象)的原因是 FetchType.EAGER
Question
重物这是因为在 "致一 "的联想中,。fetch = FetchType.EAGER
是默认使用的。
那么,懒惰加载就是答案,你必须设置为 FetchType.LAZY
在 Response
覆盖 question
领域为 @ManyToOne(fetch = FetchType.LAZY)
以便停止循环。
将问题更新为 FetchType.LAZY
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id")
private Question question;