Question

I'm trying to convert this SQL to HQL, but I found no way to do that left join.

SELECT
    mdcs_causa.id_causa,
    usuarios.ds_usuario,
    usuarios.setor,
    empresas.ds_empresa,
    itens_controle.id_item_controle,
    itens_controle.ds_item,
    itens_controle.ds_indicador,
    itens_controle.ds_cliente,
    itens_controle.desdobramento,
    itens_controle.auxiliar,
    itens_controle.bmk_nome,
    itens_controle.bmk_vlr,
    m.status,
    m.medido,
    m.medicao,
    m.fca,
    m.am_cronico,
    m.ac_cronico,
    m.ap_cronico,
    m.id_medicoes as idmedicao,
    itens_controle.prioridade
FROM
    mdcs
    INNER JOIN mdcs_causa 
        ON mdcs.id_mdc = mdcs_causa.id_mdc
    INNER JOIN itens_controle
        ON mdcs_causa.Id_Item_Controle = itens_controle.id_item_controle
    INNER JOIN usuarios
        ON usuarios.id_usuario = itens_controle.id_usuario
    INNER JOIN empresas 
        ON empresas.id_empresa = usuarios.id_cliente_tabela
    LEFT JOIN medicoes m 
        ON (
            itens_controle.id_item_controle = m.id_item_controle
            and
            m.nm_ano = 2013 
            and 
            m.nm_periodo = 2 
            and 
            mdcs_causa.id_mdc = mdcs.id_mdc
        )
    WHERE
        mdcs.id_mdc = 5077

I thought I could put the nm_ano and nm_periodo conditions in the where clause, with an OR to m.id_item_controle is null but this OR condition didn't seem to work, even in SQL.

Another approach was to left join a sub query. That worked in SQL, but I think HQL doesn't support that.

Was it helpful?

Solution

It can be done with with keyword:

... 
left join itens_controle.medicoes m with (m.nm_ano = 2013 and m.nm_periodo = 2 and mdcs_causa.id_mdc = mdcs.id_mdc)
where mdcs.id_mdc = 5077

Instead of explicit values you should supply them as arguments.

OTHER TIPS

Read this in this pdf left join is used as below:

Query getEBills =session.createQuery( session.createQuery( from " EBill ebill EBill
ebill
left join ebill.accountTransaction where
ebill.balance > 500";
Li li f l bi i li () ist listOfRowValues = getDebitTransactions.list();
for (Object[] singleRowValues : listOfRowValues) {
// pull off the EBill // pull off the EBill
EBill ebill = (EBill)singleRowValues[0];

I dont have any experience in HQL, i have pasted what i have found for you. hope this will help you in any way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top