-->
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.  [ 4 posts ] 
Author Message
 Post subject: [HQL] SQL translation adding multiple joins
PostPosted: Tue Oct 18, 2011 5:07 am 
Newbie

Joined: Tue Sep 27, 2011 4:38 am
Posts: 5
All,

We have just migrate from Hibernate 3.2.5 (GA) to Hibernate 3.6.7 (Final).
After updating dialect (we were using Sybase 12.5 with SybaseDialect and migrated to Sybase ASE 15 with SybaseASE15Dialect) and 2-3 Maven dependances (slf4j-log12,...) everything (almost...) seems to be working.

But, we discovered, with an initially huge query, that this version of Hibernate duplicates some joins (inner joins in our case), which may lead to overtake Sybase (specifications...) limits of 50 tables/views access with a single query.

Without taking care of the initial query which could certainly be improved, as a general result, all queries have more joins meaning all queries take more time. Which is not exactly what we expected when we started the migration!

Below is a sample case to illustrate this. (I'm french so fields in my DB are in french... but to help you understanding the sample, an Agriculteur is a Farmer, the EtatAgriculteur is the State of the farmer (from a professional point of view -does he grow cows, does he cultivate cereals, etc...) and the Parcelle are Field (to cultivate cereals, to grow cows...)
Relationnal point-of-view : An Agriculteur may have several EtatAgriculteur (many-to-one relation) while this Agriculteur may have 'n' Parcelle. A Parcelle may be owned only by one Agriculteur. For the Agriculteur, this is a one-to-many relation.
The query below, is trying to retrieve all Parcelle, associating with them the Agriculteur and their EtatAgriculteur.

I used P6Spy to catch the SQL produced by Hibernate after translating the HQL query.

Initial HQL query:
Code:
from Parcelle p inner join p.agriculteur as agri inner join p.agriculteur.etatAgriculteur as etat  where (p.agriculteur.agrCdn = agri.agrCdn and agri.etatAgriculteur.etatCdn = etat.etatCdn)


SQL translation done by Hibernate 3.2.5.GA
Code:
select parcelle0_.PAR_CDN as PAR1_36_0_, agriculteu1_.AGR_CDN as AGR1_27_1_, etatagricu3_.ETAT_CDN as ETAT1_33_2_, parcelle0_.PAR_LIB_LB as PAR2_36_0_, parcelle0_.PAR_SURFACE_INT as PAR3_36_0_, parcelle0_.PAR_CADAST_LB as PAR4_36_0_, parcelle0_.AGR_CDN as AGR5_36_0_, agriculteu1_.AGR_NOM_LB as AGR2_27_1_, agriculteu1_.AGR_PRENOM_LB as AGR3_27_1_, agriculteu1_.AGR_ADR1_LB as AGR4_27_1_, agriculteu1_.AGR_ADR2_LB as AGR5_27_1_, agriculteu1_.AGR_ACTIF_ON as AGR6_27_1_, agriculteu1_.AGR_CODPOS_LB as AGR7_27_1_, agriculteu1_.TYPE_CDN as TYPE8_27_1_, agriculteu1_.ETAT_CDN as ETAT9_27_1_, agriculteu1_.AGR_PERE_CDN as AGR10_27_1_, agriculteu1_.COM_CP_RFA as COM11_27_1_, agriculteu1_.COM_INSEE_RFA as COM12_27_1_, agriculteu1_.JUR_CDN as JUR13_27_1_, etatagricu3_.ETAT_LIB_LB as ETAT2_33_2_, etatagricu3_.ETA_RFA as ETA3_33_2_
from PARCELLE parcelle0_
inner join AGRICULTEUR agriculteu1_ on parcelle0_.AGR_CDN=agriculteu1_.AGR_CDN
inner join ETAT_AGRICULTEUR etatagricu3_ on agriculteu1_.ETAT_CDN=etatagricu3_.ETAT_CDN
where parcelle0_.AGR_CDN=agriculteu1_.AGR_CDN and agriculteu1_.ETAT_CDN=etatagricu3_.ETAT_CDN


SQL translation done by Hibernate 3.6.7.Final :
Code:
select parcelle0_.PAR_CDN as PAR1_36_0_, agriculteu1_.AGR_CDN as AGR1_27_1_, etatagricu3_.ETAT_CDN as ETAT1_33_2_, parcelle0_.PAR_LIB_LB as PAR2_36_0_, parcelle0_.PAR_SURFACE_INT as PAR3_36_0_, parcelle0_.PAR_CADAST_LB as PAR4_36_0_, parcelle0_.AGR_CDN as AGR5_36_0_, agriculteu1_.AGR_NOM_LB as AGR2_27_1_, agriculteu1_.AGR_PRENOM_LB as AGR3_27_1_, agriculteu1_.AGR_ADR1_LB as AGR4_27_1_, agriculteu1_.AGR_ADR2_LB as AGR5_27_1_, agriculteu1_.AGR_ACTIF_ON as AGR6_27_1_, agriculteu1_.AGR_CODPOS_LB as AGR7_27_1_, agriculteu1_.TYPE_CDN as TYPE8_27_1_, agriculteu1_.ETAT_CDN as ETAT9_27_1_, agriculteu1_.AGR_PERE_CDN as AGR10_27_1_, agriculteu1_.COM_CP_RFA as COM11_27_1_, agriculteu1_.COM_INSEE_RFA as COM12_27_1_, agriculteu1_.JUR_CDN as JUR13_27_1_, etatagricu3_.ETAT_LIB_LB as ETAT2_33_2_, etatagricu3_.ETA_RFA as ETA3_33_2_
from PARCELLE parcelle0_
inner join AGRICULTEUR agriculteu1_ on parcelle0_.AGR_CDN=agriculteu1_.AGR_CDN
[b][u]inner join AGRICULTEUR agriculteu2_ on parcelle0_.AGR_CDN=agriculteu2_.AGR_CDN [/u][/b]
inner join ETAT_AGRICULTEUR etatagricu3_ on agriculteu2_.ETAT_CDN=etatagricu3_.ETAT_CDN
where parcelle0_.AGR_CDN=agriculteu1_.AGR_CDN and agriculteu1_.ETAT_CDN=etatagricu3_.ETAT_CDN

The second inner join (where I tried to put the U & B BBCode tags is the one added by Hbn3.6.7.

Is there anyone who could help me trying to understand why Hb,3.6.7 duplicates the joins and how we could solve this?

Thanks,
Olivier.


Top
 Profile  
 
 Post subject: Re: [HQL] SQL translation adding multiple joins
PostPosted: Tue Oct 18, 2011 6:47 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It's related to a change in HHH-4091 how implicit and explicit joins are handled.

For example, you have an explicit join join p.agriculteur as agri, so you'll need to replace 'p.agriculteur' with the alias 'agri' in all other places to avoid the duplicate join problem. You'll have to repeat this for the second join as well. Then, you'll probably also notice that the where part is not really needed (or not doing what you think it is doing) because it will be: where (agri.agrCdn = agri.agrCdn and etat.etatCdn = etat.etatCdn) which is always true.


Top
 Profile  
 
 Post subject: Re: [HQL] SQL translation adding multiple joins
PostPosted: Tue Oct 18, 2011 8:56 am 
Newbie

Joined: Tue Sep 27, 2011 4:38 am
Posts: 5
Thank you very much for your accurate reply. Let me give it a try and tell you how it goes.

Btw, as I told it was a sample query, written only for the purpose of providing something easy to read in this post. And yes, you're right, the where clause is absolutely not needed (I noticed that... but too late!). My bad on this one but... never mind, with or without the where clause the result is always the same (as expected with your explanation).

Brb with an answer concerning the new result.


Top
 Profile  
 
 Post subject: Re: [HQL] SQL translation adding multiple joins
PostPosted: Tue Oct 18, 2011 9:07 am 
Newbie

Joined: Tue Sep 27, 2011 4:38 am
Posts: 5
Hello I'm back.

Well, you were right nordborg.

Here is my new HQL :
Code:
from Parcelle p inner join p.agriculteur as agri inner join agri.etatAgriculteur as etat


And here is the new SQL translation done by Hbn 3.6.7 :
Code:
select parcelle0_.PAR_CDN as PAR1_36_0_, agriculteu1_.AGR_CDN as AGR1_27_1_, etatagricu2_.ETAT_CDN as ETAT1_33_2_, parcelle0_.PAR_LIB_LB as PAR2_36_0_, parcelle0_.PAR_SURFACE_INT as PAR3_36_0_, parcelle0_.PAR_CADAST_LB as PAR4_36_0_, parcelle0_.AGR_CDN as AGR5_36_0_, agriculteu1_.AGR_NOM_LB as AGR2_27_1_, agriculteu1_.AGR_PRENOM_LB as AGR3_27_1_, agriculteu1_.AGR_ADR1_LB as AGR4_27_1_, agriculteu1_.AGR_ADR2_LB as AGR5_27_1_, agriculteu1_.AGR_ACTIF_ON as AGR6_27_1_, agriculteu1_.AGR_CODPOS_LB as AGR7_27_1_, agriculteu1_.TYPE_CDN as TYPE8_27_1_, agriculteu1_.ETAT_CDN as ETAT9_27_1_, agriculteu1_.AGR_PERE_CDN as AGR10_27_1_, agriculteu1_.COM_CP_RFA as COM11_27_1_, agriculteu1_.COM_INSEE_RFA as COM12_27_1_, agriculteu1_.JUR_CDN as JUR13_27_1_, etatagricu2_.ETAT_LIB_LB as ETAT2_33_2_, etatagricu2_.ETA_RFA as ETA3_33_2_
from PARCELLE parcelle0_
inner join AGRICULTEUR agriculteu1_ on parcelle0_.AGR_CDN=agriculteu1_.AGR_CDN
inner join ETAT_AGRICULTEUR etatagricu2_ on agriculteu1_.ETAT_CDN=etatagricu2_.ETAT_CDN


Thanks a lot. You really helped me. I was just driving crazy with that problem.

Btw : to be absolutely honnest, we are a framework provider, and our framework encapsulates Hibernate. Our framework is used by more than 50 projects... and now I have to tell them they must all have a look to their HQL queries and maybe they must rewrite them. It will cost a lot of money and might be a killer for upgrading to the new version of our framework...

We'll see. But now I have both an answer and a solution.

Thanks again.


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