如何翻开' on'自我的内在联合很多很多

时间:2015-11-10 01:02:40

标签: java sql hibernate jpa

我有一个与自己有关的实体

@OrderBy("ordenacao ASC")
@JoinTable(name = "funcionarios_has_funcionarios", joinColumns = @JoinColumn(name = "FUNCIONARIOS_ID"), inverseJoinColumns = @JoinColumn(name = "FUNCIONARIOS_ID1"))
@ManyToMany(cascade = { PERSIST, MERGE })
public List<Funcionarios> getFuncionariosId1s() {
    return funcionariosId1s;
}

我有这个sql来运行

"SELECT  f.id,f.nome, COALESCE (SUM(flp.litros * flp.valor),0) as total, "
                + "(select COALESCE (SUM(v.valor),0) from Vales as v where v.funcionario = f and v.status = 'PENDENTE') as vales, "
                + "(select COALESCE (SUM(c.valor),0) from Convenio as c where c.funcionarios = f  and c.statusconveniado = 'PENDENTE') as convenios "
                + "FROM Fornecimentoleiteprodutor as flp "
                + "INNER JOIN flp.produtor as f "
                + "INNER JOIN f.funcionariosId1s as mp "
                + "where mp.id= 2"
                + "GROUP BY f",`

她以java的方式执行

`select
        funcionari1_.ID as col_0_0_,
        funcionari1_.NOME as col_1_0_,
        coalesce(sum(fornecimen0_.LITROS*fornecimen0_.VALOR),
        0) as col_2_0_,
        (select
            coalesce(sum(vales4_.VALOR),
            0) 
        from
            vales vales4_,
            funcionarios funcionari5_ 
        where
            vales4_.FUNCIONARIOS_ID=funcionari5_.ID 
            and vales4_.FUNCIONARIOS_ID=funcionari1_.ID 
            and vales4_.STATUS='PENDENTE') as col_3_0_,
        (select
            coalesce(sum(convenio6_.VALOR),
            0) 
        from
            convenio convenio6_,
            funcionarios funcionari7_ 
        where
            convenio6_.FUNCIONARIOS_ID=funcionari7_.ID 
            and convenio6_.FUNCIONARIOS_ID=funcionari1_.ID 
            and convenio6_.STATUSCONVENIADO='PENDENTE') as col_4_0_ 
    from
        fornecimentoleiteprodutor fornecimen0_ 
    inner join
        funcionarios funcionari1_ 
            on fornecimen0_.PRODUTOR_ID=funcionari1_.ID 
    inner join
        funcionarios_has_funcionarios funcionari2_ 
            on funcionari1_.ID=funcionari2_.FUNCIONARIOS_ID 
    inner join
        funcionarios funcionari3_ 
            on funcionari2_.FUNCIONARIOS_ID1=funcionari3_.ID 
    where
        funcionari3_.ID=2 
    group by
        funcionari1_.ID`

但是我需要&#39;&#39;第二个内部联接是不同的,这样

inner join
        funcionarios_has_funcionarios funcionari2_ 
            on funcionari1_.ID=funcionari2_.FUNCIONARIOS_ID1

我需要&#39; on&#39;关联连接表的第二列 我该如何解决这个问题

1 个答案:

答案 0 :(得分:0)

如果你想控制内连接,请给出你自己的ON子句。

目前,Hibernate正在根据自己的假设生成自己的ON子句。