Why doesn't Lazy loading work in one association?

@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 requests that are starting. one for obtaining human data, and the other for detailed human data.

In accordance with my understanding, only one request should be dismissed, which is intended to receive human data, and not for detailed human data, as I mentioned lazy loading. Why is PersonDetail data obtained with human data?

+4
source share
2 answers

Hibernate cannot proxy its own object, as is done for Sets / Lists in relation @ToMany, therefore Lazy loading does not work.

, : http://justonjava.blogspot.co.uk/2010/09/lazy-one-to-one-and-one-to-many.html

+6

PersonDetail , Person, , :

( PersonDetail), , @JoinColumn PersonDetail.

(, ) mappedBy, (@OneToOne ), , , PersonDetail Person.

, , 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...
}
0

Source: https://habr.com/ru/post/1524846/


All Articles