1

Please suggest how can I write the JPA EntityManager createQuery for the given SQL Query:

select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Java'
    union   
select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Microsoft'
4
  • I wanted provided SQL query which fits into em.createQuery( Answer ) Commented Nov 16, 2016 at 13:05
  • I mean how I can get JPQL query EntityManager.createQuery(Answer) ... Provided SQL Query Above Commented Nov 16, 2016 at 13:12
  • select bean from userbean bean where bean.organiz =: java or bean.organiz=:microsoft and bean.status_2 =: allocated or bean.status_2. =: bench --> this will return to you list list of size will be your answer , honestly dont understand what you mean Commented Nov 16, 2016 at 13:19
  • I mean can you modify SQL given query above into JPQL query EntityManager.createQuery( Answer ) Commented Nov 16, 2016 at 13:22

1 Answer 1

2

You can use a native sql query with jpa :

Query q = em.createNativeQuery("select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Java'
    union   
select
    sum(case when Status_2 = 'Allocated' then 1 else 0 end) as Allocated,
    sum(case when Status_2 = 'Bench' then 1 else 0 end) as Bench
    from userbean where Organizational_Unit = 'SIDG Microsoft'");

List<Object[]> result= q.getResultList();

// for each line retrieved
for (Object[] currentLine : result) {
    System.out.println("Allocated=" + currentLine[0]
                    + ", Bench=" + currentLine[1] ;
}

No guarantee but you may test.

Sign up to request clarification or add additional context in comments.

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.