[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3045) Duplicated column aliases in scalar query

Tobias Troesch (JIRA) noreply at atlassian.com
Wed Jun 9 05:26:10 EDT 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3045?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=37370#action_37370 ] 

Tobias Troesch commented on HHH-3045:
-------------------------------------


I have done the following changed in the grammarfile sql-gen.g:

Add the methode in the grammarfile sql-gen.g

protected boolean contains(AST e)
{
 return (buf.toString().contains(e.getText()));	
}


Add contains check to the methode selectExpr(AST _t) because here the SQL-Select-Columns were generated. 

public final void selectExpr(AST _t) throws RecognitionException {
		
 traceIn("selectExpr",_t);
 try { // debugging
   AST selectExpr_AST_in = (_t == ASTNULL) ? null : (AST)_t;
   AST e = null;
   AST c = null;
   AST param = null;
   AST sn = null;
			
   try {      // for error handling
     if (_t==null) _t=ASTNULL;
     switch ( _t.getType()) {
	case DOT:
	case ALIAS_REF:
	case SQL_TOKEN:
	case SELECT_EXPR:
	{
	  e = _t==ASTNULL ? null : (AST)_t;
	  selectAtom(_t);
	  _t = _retTree;
          
          ///////////////////////////////Changed in the Grammarfile
          if ( inputState.guessing==0 ) {
	    if (contains(e) == false)  out(e);
	  }
	  ///////////////////////////////Changed in the Grammarfile

          break;
	}
	case COUNT:
	{
	  count(_t);
	  _t = _retTree;
	  break;
	}
.....

	} finally { // debugging
	 traceOut("selectExpr",_t);
 }
}


Now we have no duplicates on the SQL-Select-Columns-String but we have blanks with separators.

e.g. 
SELECT  
	traceobjec1_.objectId AS objectId41_1_, 
	area3_.areaId AS areaId31_2_, 
	vehicleobj0_.id AS objectId40_0_, 
	, 
	,
	vehicleobj0_1_.dbVersionControl AS dbVersio2_40_0_, 
...

Thus we need to change the methode separator in the grammarfile sql-gen.g

protected void separator(AST n, String sep) {
 if (n.getNextSibling() != null) {

   ///////////////////////////////Changed in the Grammarfile
   if (buf.length() > sep.length()) {

     if (!buf.substring(buf.length()- sep.length(), buf.length()).equals(sep)) {
	out(sep);
     }
   }
   else
   {
   ///////////////////////////////Changed in the Grammarfile				
     out(sep);
   }
  }
}

We check if a string is added before an separator is added. So that's all.
Is it possible to do some kind of that fix in the next hibernate versions or do you fix the grammar?

> Duplicated column aliases in scalar query
> -----------------------------------------
>
>                 Key: HHH-3045
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3045
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.2.5
>         Environment: Hibernate 3.2.4SP1, 3.2.5GA
> Oracle 10g
>            Reporter: Anderson Souza
>            Priority: Critical
>         Attachments: ddl-tables.sql, dominio.zip
>
>
> The HQL bellow works fine, but when I add a call to a funcion in the select clause the SQL generated has duplicated aliases and it generate error on query execution, because the query is paginated.
> {code}
> select	a
> from 	Area a 
> 	inner join fetch a.horarioPadrao
> 	left outer join fetch a.horarioTemporario as hrtemp
> {code}
> The SQL generated for this query is:
> {code}
> select * from (
>   select  area0_.cod_local as cod1_15_0_, 
>           horarioare1_.cod_area as cod1_6_1_, 
>           horarioare2_.cod_area as cod1_7_2_, 
>           area0_.des_local as des2_15_0_, 
>           area0_.sig_local as sig3_15_0_, 
>           area0_.flg_extinto as flg4_15_0_, 
>           horarioare1_.hr_hora_ini as hr2_6_1_, 
>           horarioare1_.hr_hora_fim as hr3_6_1_, 
>           horarioare1_.flg_fim_semana as flg4_6_1_, 
>           horarioare2_.hr_hora_ini as hr2_7_2_, 
>           horarioare2_.hr_hora_fim as hr3_7_2_, 
>           horarioare2_.flg_fim_semana as flg4_7_2_, 
>           horarioare2_.dat_ini_vigencia as dat5_7_2_, 
>           horarioare2_.dat_fim_vigencia as dat6_7_2_ 
>   from    esq_fp.tb_local area0_ 
>           inner join tb_horario_local_padrao horarioare1_ on area0_.cod_local=horarioare1_.cod_area 
>           left outer join tb_horario_local_temporario horarioare2_ on area0_.cod_local=horarioare2_.cod_area 
>   where   1=1 
>   order by area0_.sig_local ) 
> where rownum <= ?
> {code}
> The scalar HQL query that generate duplicated column names is:
> {code}
> select	a, count(*)
> from 	Area a 
> 	inner join fetch a.horarioPadrao
> 	left outer join fetch a.horarioTemporario as hrtemp
> {code}
> The SQL generated is:
> {code}
> select * from (
>   select  area0_.cod_local as col_0_0_, 
>           count(*) as col_1_0_, 
>           horarioare1_.cod_area as cod1_6_1_, 
>           horarioare2_.cod_area as cod1_7_2_, 
>           area0_.cod_local as cod1_15_0_, 
>           horarioare1_.cod_area as cod1_6_1_, 
>           horarioare2_.cod_area as cod1_7_2_, 
>           area0_.des_local as des2_15_0_, 
>           area0_.sig_local as sig3_15_0_, 
>           area0_.flg_extinto as flg4_15_0_, 
>           horarioare1_.hr_hora_ini as hr2_6_1_, 
>           horarioare1_.hr_hora_fim as hr3_6_1_, 
>           horarioare1_.flg_fim_semana as flg4_6_1_, 
>           horarioare2_.hr_hora_ini as hr2_7_2_, 
>           horarioare2_.hr_hora_fim as hr3_7_2_, 
>           horarioare2_.flg_fim_semana as flg4_7_2_, 
>           horarioare2_.dat_ini_vigencia as dat5_7_2_, 
>           horarioare2_.dat_fim_vigencia as dat6_7_2_ 
>   from    esq_fp.tb_local area0_ 
>           inner join tb_horario_local_padrao horarioare1_ on area0_.cod_local=horarioare1_.cod_area 
>           left outer join tb_horario_local_temporario horarioare2_ on area0_.cod_local=horarioare2_.cod_area 
>   where 1=1 
>   order by area0_.sig_local )
> where rownum <= ?
> {code}
> As you can see the aliases cod1_6_1_ and  cod1_7_2_ are repeated and this repetition breaks the paginated query, beacause the main query appears in the from clause.
> The HBM's and classes are attached.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list