-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: OneToOne mappings always eager fetch even when set to LAZY
PostPosted: Mon Oct 20, 2008 5:40 pm 
Newbie

Joined: Mon Sep 08, 2008 10:45 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2

Hi,

I have two entity classes that have a one-to-one relationship. This is how I have them mapped:

Code:
@Entity
public Person
{
  private Student student;
   
  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY,  optional = true)
  @JoinColumn(name = "person_id")
  public Student getStudent() {
    return student;
  }
  public void setStudent(Student student) {
    this.student = student;
  }
}

@Entity
public Student
{
    private Person person;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "person_id")   
    public Person getPerson()
    {
        return person;
    }

    public void setPerson(Person person)
    {
        this.person = person;
    }
}


Whenever I perform a query involving Person, it will do a separate select to retrieve Student even though I have specified lazy fetching. This is a performance problem for me since I don't want to get Student every time.

Any ideas why this is happening and how I can make it not eager fetch the Students?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 20, 2008 5:55 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is impossible to have a one-to-one relation lazy from both sides. Basically because Hibernate doesn't know if the relation exists from the side that is nullable (in your case the Person side) without having to query the other table. Eg. Hibernate needs to know if it should call Person.setStudent(null) or Person.setStudent(proxyOfStudent).

See this previous post for possible workarounds: http://forum.hibernate.org/viewtopic.php?t=991677


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.