-->
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.  [ 5 posts ] 
Author Message
 Post subject: hibernate not using joins to fetch ToOne eager asociations
PostPosted: Mon Mar 13, 2006 6:11 am 
Newbie

Joined: Wed Dec 28, 2005 2:44 pm
Posts: 8
If one has an entity such as:

@Entity
@Table(name="main")
public class MainEntity {

private int id;
private Code1 code1;
private Code2 code2;
private Code3 code3;

@Id
public int getId(){return id;}
public void setId(int pId){id = pId;}

@ManyToOne(targetEntity=Code1.class)
@JoinColumn(name="code1")
public int getCode1(){return code1;}
public void setCode1(Code2 pCode1){code1 = pCode1;}

@ManyToOne(targetEntity=Code2.class)
@JoinColumn(name="code2")
public int getCode2(){return code2;}
public void setCode2(Code2 pCode2){code2 = pCode2;}

@ManyToOne(targetEntity=Code3.class)
@JoinColumn(name="code3")
public int getCode3(){return code3;}
public void setCode3(Code3 pCode3){code3 = pCode3;}
}

while Code1, Code2 and Code3 are simple code-desc entities (having only to simple properties, code and desc), it is expected that if one tries to fetch all the MainEntity objects (using "Select a From MainEntity a" query, for example) there will be only one sql-select statement
(
SELECT m.id, m.code1, m.code2, m.code3, c1.code, c1.desc, c2.code, c2.desc, c3.code, c3.desc

FROM main m
LEFT OUTER JOIN code1 c1 ON m.code1 = c1.code
LEFT OUTER JOIN code2 c2 ON m.code2 = c2.code
LEFT OUTER JOIN code3 c3 ON m.code3 = c3.code
)

Instead, there are 3*n+1 sql-select statements (where n is the number of rows in the main table). One from the main table, and for each row, one from each codes table.

If one uses a query from

SELECT a FROM MainEntity a JOIN FETCH a.code1 JOIN FETCH a.code2 JOIN FETCH a.code3

it works correctly, using one sql-statements.
Is this a bug? how should it be fixed?

Hibernate version:3.1.rc2

Name and version of the database you are using:Oracle9i


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 8:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
this is the expected behavior, EJBQL ignores the fetching (select/join) strategy of the entities

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 9:44 am 
Newbie

Joined: Wed Dec 28, 2005 2:44 pm
Posts: 8
So hibernate is supposed to execute more than 100 accesses to the DB if you fetch a table of 35 rows with three ManyToOne links? What happened to efficiency? If I have tables with several thousands rows, I can't use hibernate? Does it make sense?
Would you (the development team) like a ptach to change this behaviour?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 8:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
select p from Person p left join fetch p.country left join fetch p.citizenship will do 1 query to load Person and 2 many to one

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 8:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No, no patch is needed.

_________________
Emmanuel


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