如何在JPA中不被子女的子女......等?

问题描述 投票:0回答:1

我可能误解了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],它有一个响应列表,但每个问题都有一个问题,它也有一个响应列表,等等... ..:

enter image description here

我怎样才能指定连接列键而不需要把整个对象和他的所有子对象等等?

非常感谢您的帮助!

Joss

java jpa join persistence one-to-many
1个回答
1
投票

在你所做的配置中,流按以下顺序检索数据。

  • Question 对象
  • 相关实体(即 Response 对象)的原因是 FetchType.EAGER
  • Question重物
  • 举步维艰

这是因为在 "致一 "的联想中,。fetch = FetchType.EAGER 是默认使用的。

那么,懒惰加载就是答案,你必须设置为 FetchType.LAZYResponse 覆盖 question 领域为 @ManyToOne(fetch = FetchType.LAZY) 以便停止循环。


0
投票

将问题更新为 FetchType.LAZY

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id")
private Question question;
© www.soinside.com 2019 - 2024. All rights reserved.