-->
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.  [ 7 posts ] 
Author Message
 Post subject: n+1 selects problem for One-to-One association
PostPosted: Mon Jan 25, 2010 8:59 am 
Newbie

Joined: Mon Jan 25, 2010 8:27 am
Posts: 3
Hello all,
I have this simple One-to-One association:
Master entity:
Code:
@Entity @Table(name = "master")
public class Master implements java.io.Serializable {
  @Id @Column(name = "ID", unique = true, nullable = false, precision = 9, scale = 0)
  private int id;
  @OneToOne(fetch = FetchType.LAZY, mappedBy = "master")
  private Detail detail;
  public Master() { }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public Detail getDetail() {
    return detail;
  }
  public void setDetail(Detail detail) {
    this.detail = detail;
  }
}

Detail Entity
Code:
@Entity @Table(name = "detail")
public class Detail implements java.io.Serializable {
  @Id @Column(name = "ID", unique = true, nullable = false, precision = 9, scale = 0)
  private int id;
  @ManyToOne(fetch = FetchType.LAZY, optional = false)   
  @JoinColumn(name = "master_id")
  private Master master; 
  public Detail() { }
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public Master getMaster() {
    return master;
  }
  public void setMaster(Master master) {
    this.master = master;
  }
}

When I run this query:
Code:
SELECT e FROM Master e LEFT JOIN FETCH e.detail

Then correctly only this query is executed:
Code:
Hibernate:
    select
        master0_.ID as ID1_0_,
        detail1_.ID as ID0_1_,
        detail1_.master_id as master2_0_1_
    from
        master master0_
    left outer join
        detail detail1_
            on master0_.ID=detail1_.master_id

But when I run the following:
Code:
SELECT e FROM Detail e LEFT JOIN FETCH e.master

I get the dreaded n+1 selects problem:
Code:
Hibernate:
    select
        detail0_.ID as ID0_0_,
        master1_.ID as ID1_1_,
        detail0_.master_id as master2_0_0_
    from
        detail detail0_
    left outer join
        master master1_
            on detail0_.master_id=master1_.ID
Hibernate:
    select
        detail0_.ID as ID0_0_,
        detail0_.master_id as master2_0_0_
    from
        detail detail0_
    where
        detail0_.master_id=?
Hibernate:
    select
        detail0_.ID as ID0_0_,
        detail0_.master_id as master2_0_0_
    from
        detail detail0_
    where
        detail0_.master_id=?
...

This looks like I bug to me, because with the first query all the information has been fetched, and the following n selects are not needed. What do you think? For your info, I'm using Hibernate core 3.3.1.GA and annotations/entitymanager version 3.4.0.GA (the libraries comming with JBoss 5)

Thank you in advance,
Kostas


Top
 Profile  
 
 Post subject: Re: n+1 selects problem for One-to-One association
PostPosted: Mon Jan 25, 2010 1:56 pm 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Please make the changes as following and try . Very first thing you have to move the annotation at method (getter methods) level
:-
Code:
@Entity @Table(name = "master")
public class Master implements java.io.Serializable {
-----
----

public void setDetail(Detail detail) {
    this.detail = detail;
    details.setMaster(this); //This line need to be added
  }

}


Top
 Profile  
 
 Post subject: Re: n+1 selects problem for One-to-One association
PostPosted: Thu Jan 28, 2010 10:36 am 
Newbie

Joined: Mon Jan 25, 2010 8:27 am
Posts: 3
Thanks for the reply but:

1. Moving the annotations to the getters did not make any differece
2. I'm not using the setters in my example so I don't see any point in changing them

Regards,
Kostas

parmendratyagi wrote:
Please make the changes as following and try . Very first thing you have to move the annotation at method (getter methods) level
:-
Code:
@Entity @Table(name = "master")
public class Master implements java.io.Serializable {
-----
----

public void setDetail(Detail detail) {
    this.detail = detail;
    details.setMaster(this); //This line need to be added
  }

}


Top
 Profile  
 
 Post subject: Re: n+1 selects problem for One-to-One association
PostPosted: Thu Jan 28, 2010 11:45 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
It is.. Actually moving the annotation to getter method enable Hibernate to set the property value through setter, other wise (As per current annotation) it will set value directly on the field.(Which actually cause the problem).

And you have to use setter method to solve this problem.. should be implemented as posted in last post...


Top
 Profile  
 
 Post subject: Re: n+1 selects problem for One-to-One association
PostPosted: Thu Jan 28, 2010 12:12 pm 
Newbie

Joined: Mon Jan 25, 2010 8:27 am
Posts: 3
Please read my original post again. I do not mention anywhere that I use hibernate to save the values in the tables, nor that I have any problem saving these values in the first place! The problem occurs when I query existing values.

More importantly, I have tested your suggestion and it does not work. The HQL query still produces n+1 SQL selects. You can test it yourself if you do not believe me.


Top
 Profile  
 
 Post subject: Re: n+1 selects problem for One-to-One association
PostPosted: Mon Feb 01, 2010 1:05 pm 
Beginner
Beginner

Joined: Fri Jan 23, 2009 10:34 am
Posts: 25
Location: Switzerland
According to viewtopic.php?p=2380205 , lazy loading of @OneToOne associations does not work.
You may also have a look to the following threads:
viewtopic.php?f=1&t=993146
http://opensource.atlassian.com/project ... e/HHH-2753


Top
 Profile  
 
 Post subject: Re: n+1 selects problem for One-to-One association
PostPosted: Tue Feb 02, 2010 7:25 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
I test this code with Hibernate Core 3.3.2 GA and Hibernate Annotation 3.4.0.GA. I haven't found any problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.