0

Same query working in SQL

Select count(tbl_leads.lead_Status_Id) , tbl_LeadStatus.LeadStatus_Type FROM tbl_LeadStatus LEFT JOIN tbl_leads ON tbl_LeadStatus.LeadStatus_id = tbl_leads.lead_Status_Id GROUP BY tbl_LeadStatus.LeadStatus_Type;

Hibernate Query

Select s.LeadStatus_Type, count(l.status) FROM Status s "
                + "LEFT JOIN Lead l ON l.status = s.LeadStatus_id "
                + "GROUP BY s.LeadStatus_Type"

Expecting output is this

   Count  LeadStatus_Type
    '0'   'Cancelled' 
    '0'   'In-Progress' 
    '1'   'New' 
    '0'   'Sold' 
    '0'   'UnAssigned' 

And HQL return this

'1', 'New' 
6
  • is LeadStatus_type column is nullable? And if so, we expecting count on it from the hibernate explicitely? Commented Jan 2, 2018 at 7:06
  • '0', 'Cancelled' '0', 'In-Progress' '1', 'New' '0', 'Sold' '0', 'UnAssigned' I need this and I get this 1 "New" because there is another count is null Commented Jan 2, 2018 at 7:08
  • Please elaborate the above '0', 'Cancelled' '0',...? And 'UnAssigned' is special marker value representing NULL; in that case you can use the coalesc function in HQL in select part as coalesc(s.LeadStatus_Type, 'UnAssigned') Commented Jan 2, 2018 at 7:12
  • Please show us sample data and your expected output. Commented Jan 2, 2018 at 7:14
  • I need the 0 count when no leads found to LeadStatus_id . it not showing that record in result Commented Jan 2, 2018 at 7:18

1 Answer 1

1

Your join condition looks off. In HQL we join from an entity to the other entity which exists as a property of the first entity. Most importantly, there is no ON clause as that relationship is already known within Hibernate. Try the following:

SELECT s.LeadStatus_Type, COUNT(l.status)
FROM Status s
LEFT JOIN s.LeadStatus_id l
GROUP BY s.LeadStatus_Type
Sign up to request clarification or add additional context in comments.

6 Comments

Not working! Null pointer Exception and also you don't define l
@HarpreetSingh The query in your question is raw SQL, not HQL. Are you looking for HQL or for raw SQL?
SQL query is running fine and give the desired output. I Need HQL query which returns 0 if count not found. The HQL query is also running but null value count not showing in it
Any Idea about this?
The query should be working. Tell me the error and/or the error output.
|

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.