[英]How I can write JPA query for given SQL query
請建議如何為給定的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'
您可以在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] ;
}
不保證,但您可以測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.