本文介绍了为什么懒加载不能在一对一的关联中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@Entity
public class Person {
@Id
@GeneratedValue
private int personId;
@OneToOne(cascade = CascadeType.ALL,mappedBy =person,fetch = FetchType.LAZY)
私人PersonDetail personDetail;
// getters and setters
}
@Entity
public class PersonDetail {
@Id
@GeneratedValue
private int personDetailId;
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private Person person;
// getters and setters
}
当我做的时候
Person person1 =(Person)session.get(Person.class,1);
我看到两个查询被触发。一个用于获取人员数据,另一个用于获取人员详细信息。
根据我的理解,只有1个查询应该被触发,用于获取人员数据而不是人员详细数据,正如我提到的
懒惰加载。为什么personDetail数据与人员数据一起被获取?
解决方案 Hibernate无法代理自己的对象,因为它对Sets / Lists @ToMany
关系,所以延迟加载不起作用。
我认为这个链接可以帮助您理解您的问题:
@Entity
public class Person {
@Id
@GeneratedValue
private int personId;
@OneToOne(cascade=CascadeType.ALL, mappedBy="person", fetch=FetchType.LAZY)
private PersonDetail personDetail;
//getters and setters
}
@Entity
public class PersonDetail {
@Id
@GeneratedValue
private int personDetailId;
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private Person person;
//getters and setters
}
when i do
Person person1=(Person)session.get(Person.class, 1);
i see two queries being fired. one for fetching person data and another for person detail data.
As per my understanding only 1 query should have been fired that is for fetching person data not for person detail data as i have mentioned lazy loading. Why personDetail data is getting fetched along with person data ?
解决方案
Hibernate cannot proxy your own object as it does for Sets / Lists in a @ToMany
relation, so Lazy loading does not work.
I think this link could be useful to understand your problem: http://justonjava.blogspot.co.uk/2010/09/lazy-one-to-one-and-one-to-many.html
这篇关于为什么懒加载不能在一对一的关联中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!