Why Lazy loading not working in one to one association?

hibernate, java, lazy-loading

Solution

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

Problem

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

Original source

Related problems