1

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'
        )
4
  • Column Completed is it VARCHAR or BIT ?? Commented Mar 3, 2014 at 20:58
  • PostGresql says it is boolean. Commented Mar 3, 2014 at 20:59
  • then it should be 'false'. i.e enclosed inside single quotes '. Check out this link here. Commented Mar 3, 2014 at 21:02
  • i have already tried this. I thinh that the problem comes up when I try to check value on unexisting object Commented Mar 3, 2014 at 21:05

2 Answers 2

6

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)
Sign up to request clarification or add additional context in comments.

11 Comments

Let me know which one works for you and I'll update the answer accordingly.
I've tried this, but it have the same output as ealier. im quite sure that problem is with: 'c.connectedFinancialAnalyst.completed is null or c.connectedFinancialAnalyst.completed = false'
Try doing a left join.
It returned expected number of rows, now i have to deal with returned object's becouse they r objects of other type
If you have select c from Customer c, then you will get the Customer object. Is that not what you want?
|
0

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 ^^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.