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

M Sach
  • 30,322
  • 72
  • 198
  • 300
  • Check [this](http://stackoverflow.com/questions/6290202/hibernate-onetoone-lazy-loading-and-cascading) thread, that explains it well. – aljipa Feb 01 '14 at 15:09

2 Answers2

8

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

gipinani
  • 12,376
  • 10
  • 50
  • 80
0

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

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

You have already correctly defined the inverse side of the relationship (the entity that is not the owner of the relationship) with the mappedBy attribute that was added to the association annotation (@OneToOne in your case) to make the relationship bi-directional, which means that the associated PersonDetail may be reached from a Person instance.

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

@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...
}
Sean Mickey
  • 7,318
  • 2
  • 27
  • 56
  • "Based on the code in your question, my guess is that the backing table for the Person entity contains a foreign key column that references the PersonDetail entity. If that is correct, it looks like you have 2 problems:" Its other way around. See http://stackoverflow.com/questions/20934130/owning-side-vs-non-owning-side-in-hibernate-and-its-usage-in-reference-of-mapped/20934226#20934226 – M Sach Feb 02 '14 at 01:53
  • Okay, I have updated my answer based on your comment. I think adding the @JoinColumn annotation is all you should have to change. – Sean Mickey Feb 02 '14 at 02:08
  • @MSach I have updated my answer to align with the information in your comment. If your question is answered with the other SO question you reference, you may want to remove this question or post your own answer that references that question. That way, it will still be helpful to others that find their way to this question while looking for help with a similar problem. – Sean Mickey Feb 02 '14 at 02:16
  • 1
    I don't think the @ JoinColumn annotation is even necessary unless you need to give it props like nullable = false, updatable = false, etc. JPA can already tell it's not a @ Basic column... so the @ JoinColumn annotation probably won't do anything. – Amalgovinus Feb 27 '17 at 19:04