请建议如何为给定的SQL查询编写JPA EntityManager createQuery
:
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'
答案 0 :(得分:2)
您可以使用jpa的本机SQL查询:
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] ;
}
不保证,但您可以进行测试。