Java 为什么延迟加载在一对一关联中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21499580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:01:25  来源:igfitidea点击:

Why Lazy loading not working in one to one association?

javahibernatelazy-loading

提问by M Sach

@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 ?

根据我的理解,只有 1 个查询应该被触发,用于获取个人数据而不是个人详细信息数据,正如我提到的延迟加载。为什么 personDetail 数据与 person 数据一起被获取?

采纳答案by gipinani

Hibernate cannot proxy your own object as it does for Sets / Lists in a @ToManyrelation, so Lazy loading does not work.

Hibernate 不能代理您自己的对象,因为它为@ToMany关系中的 Sets/Lists所做的那样,因此延迟加载不起作用。

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

我认为此链接可能有助于了解您的问题:http: //justonjava.blogspot.co.uk/2010/09/lazy-one-to-one-and-one-to-many.html

回答by Sean Mickey

Based on your comment and since the PersonDetailentity contains a foreign key column that references the Personentity, it looks like you only have 1 problem:

根据您的评论,并且由于PersonDetail实体包含引用实体的外键列Person,因此您似乎只有 1 个问题:

Entity relationships include the concept of a relationship owner(in this case PersonDetail), which means that you want to add a @JoinColumnannotation in the PersonDetailentity.

实体关系包括关系所有者的概念(在本例中为PersonDetail),这意味着您要@JoinColumnPersonDetail实体中添加注释。

You have already correctly defined the inverse side of the relationship (the entity that is not the ownerof the relationship) with the mappedByattribute that was added to the association annotation (@OneToOnein your case) to make the relationship bi-directional, which means that the associated PersonDetailmay be reached from a Personinstance.

您已经使用添加到关联注释(在您的情况下)的属性正确定义了关系的反面(不是关系所有者的实体)以使关系双向,这意味着可以从实例访问相关联。mappedBy@OneToOnePersonDetailPerson

Given the relationship that is clarified in your comment, you should only have to make 1 change to your code as shown here:

鉴于您在评论中阐明的关系,您只需对代码进行 1 处更改,如下所示:

@Entity
public class Person {
    @Id
    @GeneratedValue
    private int personId;

    //Retain the mappedBy attribute here: 
    @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)
    //Change: add the @JoinColumn annotation here:
    @JoinColumn(name="PERSON_FK_COLUMN")
    private Person person;
    //getters and setters...
}