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 EntityCode:
@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