我可能误解了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],其中有一个响应列表,但每个问题都有一个问题,其中也有响应列表,依此类推...:
我如何在不带整个对象及其所有子代的情况下指定联接列键?
非常感谢您的帮助!
乔斯
答案 0 :(得分:1)
在配置中,流程按以下顺序检索数据:
Question
对象Response
而引起的相关实体(即FetchType.EAGER
对象)Question
再次成为对象这是因为默认情况下,在“一对一”关联中使用fetch = FetchType.EAGER
。
然后才是延迟加载,您必须将FetchType.LAZY
字段上Response
对象中的question
对象中的@ManyToOne(fetch = FetchType.LAZY)
设置为function set_pref_cookie(){
$root = $_SERVER['REQUEST_URI'];
if ($root !='/'){
setcookie('pref_sel',$root, time()+60*60*24*5, "/");
}
if (isset($_COOKIE['pref_sel'])){
header('Location:' . $_COOKIE['pref_sel']);
exit;
}
}
add_action('init','set_pref_cookie');
,以停止循环。
答案 1 :(得分:0)
将问题更新为FetchType.LAZY
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id")
private Question question;