Hibernate HQL Query with outer join

前端 未结 2 844
小蘑菇
小蘑菇 2020-12-22 02:15

I would like to ask how to change the OUTER from Informix to HQL query. Currently facing error with unexpected token:outer

SQL query:

         


        
相关标签:
2条回答
  • 2020-12-22 02:39

    The trouble you're running into is that the (obsolescent) old-style Informix OUTER join notation is not understood by other systems (such as HQL). Here's a slightly simplified version of the original Informix query:

    select a.*, b.*, d.*, e.*, f.*
      from SC_OB_TEMP_AUDIT2 a,
           SC_OB_ALLOCATION b,
           outer SC_FAC_GROUP f,
           outer (SC_OB_PROD_GROUP d, GBL_PRODUCT e)
     where a.SC_ORIG_COUNTRY = 'MY'
       and a.EXPORT_FLAG = 'N'
       and a.SC_OB_PROFILE_CODE = b.SC_OB_PROFILE_CODE
       and a.SC_ORIG_COUNTRY = b.SC_ORIG_COUNTRY
       and f.SC_ORIG_COUNTRY = b.SC_ORIG_COUNTRY
       and b.SC_ORIG_SF_GROUP_CODE = f.SC_FAC_GROUP_CODE
       and d.SC_ORIG_COUNTRY = b.SC_ORIG_COUNTRY
       and b.SC_PROD_CONT_GROUP_CODE = d.SC_PROD_GROUP_CODE
       and e.GP_CNT_CD = d.SC_PROD_CONT_CODE
     order by a.SC_TIMESTAMP, a.SC_OB_PROFILE_CODE, a.SC_DEST_COUNTRY;
    

    Primarily, I've simplified the select-list by using a.* notation for selecting the columns from each aliased table name, and I've exposed the FROM clause contents a bit more clearly.

    The tables (with aliases — but I'm going to pretend that the aliases are the table names) a and b are inner joined. That is outer joined to f; it is also independently outer joined to the result of d outer joined to e. To rewrite this so that HQL has a chance of understanding it, we're going to have to write something close to this:

    SELECT a.*, b.*, d.*, ef.*
      FROM sc_ob_temp_audit2 AS a
      JOIN sc_ob_allocation  AS b
        ON a.sc_ob_profile_code = b.sc_ob_profile_code
       AND a.sc_orig_country    = b.sc_ob_orig_country
     OUTER JOIN sc_fac_group AS f
        ON b.sc_orig_country       = f.sc_orig_country
       AND b.sc_orig_sf_group_code = f.sc_fac_group_code
     OUTER JOIN (SELECT e.*, f.*
                   FROM sc_ob_prod_group AS d
                  OUTER JOIN gbl_produce AS e
                     ON d.sc_prod_cont_code = e. gp_cnt_cd
                ) AS ef.*
        ON b.sc_orig_country         = ef.sc_orig_country
       AND b.sc_prod_cont_group_code = ef.sc_prod_group_code
     WHERE a.sc_orig_country = 'MY'
       AND a.export_flag     = 'N'
     ORDER BY a.sc_timestamp, a.sc_ob_profile_code, a.sc_dest_country;
    

    Apart from inverting the case of keywords vs database objects, this is a fairly direct translation of the original query. You will note that the outer joined sub-query is given the alias ef in this revision. This is necessary; the SQL standard mandates that such sub-queries are given an alias. You therefore may run into ambiguous column errors. You can probably fix those by by replacing the * notation in the sub-query with the exact set of column names you want to select. And, of course, you'll need to replace the * notation in the select-list with the set of columns that you want once more.

    0 讨论(0)
  • 2020-12-22 02:42

    The syntax to make a join is

    select ... from Entity1 e1
    [inner | left [outer]] join e1.entity2s e2
    [inner | left [outer]] join e2.entity3s e3
    ...
    

    Read the documentation

    0 讨论(0)
提交回复
热议问题