Bug 1092175 - [GSS] (6.3.1) HQL FromElement is not reused in some cases resulting in an additional join - HHH-9305
Summary: [GSS] (6.3.1) HQL FromElement is not reused in some cases resulting in an add...
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: JBoss Enterprise Application Platform 6
Classification: JBoss
Component: Hibernate
Version: TBD EAP 6
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: CR1
: EAP 6.3.1
Assignee: Gail Badner
QA Contact: Martin Simka
Russell Dickenson
URL:
Whiteboard:
: 1077318 (view as bug list)
Depends On: 1128278
Blocks: eap631-blockers, eap631-payload, eap63-cp01-blockers 1121630
TreeView+ depends on / blocked
 
Reported: 2014-04-28 22:03 UTC by Gary Hu
Modified: 2018-12-06 16:22 UTC (History)
6 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
: 1128278 (view as bug list)
Environment:
Last Closed: 2014-10-13 18:36:33 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)
hibernate-prjs.zip (23.88 KB, application/zip)
2014-07-10 20:26 UTC, Gary Hu
no flags Details

Description Gary Hu 2014-04-28 22:03:05 UTC
Hibernate generates incorrect sql query if more than two levels of references in hql being used.

For example:
  select e.lastName, e.department.deptName, e.title from Employee e

The following sql is generated by Hibernate:

  select
        employee0_.lastName as col_0_0_,
        department1_.deptName as col_1_0_,
        employee0_.title_id as col_2_0_,
        title2_.id as id1_,
        title2_.description as descript2_1_ 
    from
        Employee employee0_,
        Department department1_ 
    inner join
        Title title2_ 
            on employee0_.title_id=title2_.id 
    where
        employee0_.dept_no=department1_.deptNo


If the query above is being executed  "invalid identifier" error is thrown at the database such as oracle, postgres, etc.

The correct query should be like:
   select
        employee0_.lastName as col_0_0_,
        department1_.deptName as col_1_0_,
        employee0_.title_id as col_2_0_,
        title2_.id as id1_,
        title2_.description as descript2_1_ 
    from
        Employee employee0_
    inner join
        Title title2_ 
            on employee0_.title_id=title2_.id, 
        Department department1_ 
    where
        employee0_.dept_no=department1_.deptNo


Please note that, the hibernate generated sql put the line "department department1_ " prior to the inner join:
select
  ... 
  from
    Employee employee0_,
    Department department1_ 
  inner join
  ....
  where
  ...

But actually the correct sql should be like:
  select
  ... 
  from
    Employee employee0_ 
  inner join
    Title title2_ 
   on employee0_.title_id=title2_.id, 
  Department department1_ 
  where
 ...


You can find more details from https://hibernate.atlassian.net/browse/HHH-6326

Comment 1 Gary Hu 2014-06-23 19:16:19 UTC
more use cases:

@NamedQuery(name = "AgendaRecolhimento.queryPesquisa", query = "select new AgendaRecolhimento(ar.id, ar.convenio.id, ar.convenio.descricao, ar.dataReferencia, ar.numAgenda, ar.situacaoAgenda.descricao, ar.situacaoAgenda.codigoRegistro) from AgendaRecolhimento ar order by ar.dataReferencia, ar.numAgenda, ar.convenio.id"),


@NamedQuery(name = "Debito.queryPesquisa", query = "select new Debito(d.id, d.idDebitoOriginal, d.anoDebito, d.mesDebito, d.indDividaAtiva, d.vlPrincipal, d.situacaoDebito.id, d.situacaoDebito.descricao, responsavelDebito, t.nome, o.sigla, i.inscricaoImovel, i.digito) from Debito d join d.responsavelDebito as responsavelDebito left outer join d.tributoOrgao as tributoOrgao left outer join tributoOrgao.orgao o left outer join tributoOrgao.tributoArr t left outer join d.imovel as i"),


@NamedQuery(name = "TributoOrgao.queryLookup", query = "select new TributoOrgao(tor.id, tor.orgao.id, tor.orgao.descricao, tor.orgao.sigla, tor.tributoArr.id, tor.tributoArr.nome) from TributoOrgao tor")

@NamedQuery(name = "TributoReceita.queryPesquisa", query = "select new TributoReceita(tr.id, tr.tributoArr.id, tr.tributoArr.codigo, tr.tributoArr.descCompleta, tr.receitaContabil.exercicio, tr.receitaContabil.codigoReceita, tr.receitaContabil.nome, tr.tipoReceitaTributo.descricao, tr.dataInicio, tr.dataFim) from TributoReceita tr order by tr.tributoArr.codigo, tr.tributoArr.descCompleta"),

Comment 2 Gary Hu 2014-07-10 20:26:48 UTC
Created attachment 917174 [details]
hibernate-prjs.zip

Comment 5 Gail Badner 2014-07-16 06:56:22 UTC
This is a regression.

The fix for https://hibernate.atlassian.net/browse/HHH-3749 was applied to 3.2 and 3.3 branches properly, but was only partially fixed in the trunk (which ultimately was branched for 3.5).

There were 2 commits for 3.3:

15867 (https://github.com/hibernate/hibernate-orm/commit/89cf22bab6bbd5aa317ca7a1df63624bb7b677c4)
15873 (https://github.com/hibernate/hibernate-orm/commit/7b943f0089f6f90cbf836b499e2da7c1ac9e9dea)

There was only 1 commit for trunk, which corresponds to the first commit on 3.3 (15867):
15866 (https://github.com/hibernate/hibernate-orm/commit/4ed21cbce5057cfdaf637bd7e98b3788f87e035d)

When I made the changes to 4.3 from the 2nd commit on 3.3, the resulting SQL was the same as from 3.3.2.GA.

There were no failures due to this change to 4.3, but I need to check the affect on generated SQL from the tests to ensure that differences are as expected. I'll do this tomorrow.

Comment 6 Gail Badner 2014-07-16 22:06:55 UTC
I need to clarify that the regression is not in HHH-6326. That was also present in 3.3.2.GA.

The regression is that an extra join that was basically the same as an existing join was being generated . It was this extra join that was causing the situation seen with HHH-6326.

The fix gets rid of this extra join, making the generated SQL the same as it was in 3.3.2.GA.

I'll create a new Jira issue for this fix.

Comment 7 Gail Badner 2014-07-30 00:04:18 UTC
I've created a new issue for the regression: 
https://hibernate.atlassian.net/browse/HHH-9305

HHH-6326 is a long-standing issue and will not be fixed by HHH-9305; however, the fix for HHH-9305 will avoid running into HHH-6326 in most of the cases listed here.

The following HQL will still be problematic because it has >1 implicit join:
select e.lastName, e.department.deptName, e.title from Employee e

After HHH-9305 is fixed, it will be easier to workaround by making 1 or both implicit joins explicit instead. For example:

select e.lastName, e.department.deptName, e.title from Employee e inner join e.department

select e.lastName, e.department.deptName, e.title from Employee e inner join e.title

select e.lastName, e.department.deptName, e.title from Employee e inner join e.department inner join e.title

Comment 8 Gail Badner 2014-07-30 00:06:50 UTC
Gary, I've changed the target release to EAP 6.3.1.

Please add a new BZ for EAP 6.4.0.

Thanks!
Gail

Comment 9 Gail Badner 2014-07-30 07:46:42 UTC
HHH-9305 has been fixed in the upstream (4.2) branch.

Comment 12 Gail Badner 2014-08-11 21:34:56 UTC
*** Bug 1077318 has been marked as a duplicate of this bug. ***

Comment 15 Jan Martiska 2014-09-01 15:27:03 UTC
Verified in EAP 6.3.1.CR1.


Note You need to log in before you can comment on or make changes to this bug.