Pergunta

I have problem with executing following query:

from Customer c where c.connectedUserID = 1 and (c.connectedFinancialAnalyst is null or c.connectedFinancialAnalyst.completed = false)

object c.connectedFinancialAnalyst can be null. What I need is to find Customers that dont have financial analyst or property in financial analyst has specific value.

Edit: completed property is boolean in postgres and in java

Output generated by Hibernate

select
        customer0_.id as id1_0_,
        customer0_.birthDate as birthDat2_0_,
        customer0_.birthPlace as birthPla3_0_,
        customer0_.city as city4_0_,
        customer0_.email as email5_0_,
        customer0_.fatherName as fatherNa6_0_,
        customer0_.motherName as motherNa7_0_,
        customer0_.name as name8_0_,
        customer0_.pesel as pesel9_0_,
        customer0_.phoneNumber as phoneNu10_0_,
        customer0_.postalCode as postalC11_0_,
        customer0_.secondName as secondN12_0_,
        customer0_.sex as sex13_0_,
        customer0_.street as street14_0_,
        customer0_.surname as surname15_0_,
        customer0_.connectedFinancialAnalyst_id as connect17_0_,
        customer0_.connectedUserID as connect16_0_ 
    from
        Customer customer0_ cross 
    join
        FinancialAnalyst financiala1_ 
    where
        customer0_.connectedFinancialAnalyst_id=financiala1_.id 
        and customer0_.connectedUserID=1 
        and (
            customer0_.connectedFinancialAnalyst_id is null 
            or financiala1_.completed='false'
        )
Foi útil?

Solução

You need to be explicit that you want to do a left join

from Customer c 
left join c.connectedFinancialAnalyst as analyst
where c.connectedUserID = 1 and (analyst.completed is null or analyst.completed = false)

Outras dicas

I rewritten my code to Criteria and evrything works great. I suppose tkat the problem was with left join. When I used criteria i had to append Left Outter join like:

Criteria c = session.createCriteria(Customer.class, "c").createAlias("c.connectedFinancialAnalyst", "analyst", JoinType.LEFT_OUTER_JOIN);

Maybe when i would change left join at query to left outter join everything might work great ^^. But now my code seems to be a little bit more user friendly ^^

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top