2016-12-19 37 views
0

我試圖用SelectCase創建此CriteriaQuery,但出現錯誤。這是我的標準變量:如何使用標準CASE彙總模型屬性JPA

CriteriaBuilder cb; 
CriteriaQuery cq; 
Root<Stransaction> transaction; 
Join<Stransaction, Customer> customerJoin; 
Join<Customer, Country> countryJoin; 
Join<Stransaction, Supplier> supplierJoin; 
Join<Stransaction, Productfamily> familyJoin; 

在我的模型一切都通過ID的(國家,供應商,客戶,家庭)加入到交易。

加入與標準的一切後,我要的是這個(我知道這是在臉上表現一拳,我以後會解決這個問題,我只是學習標準。):

SUM(CASE WHEN COUNTRY.COUNTRYNAME = :COUNTRYNAME THEN Transaction.CFR ELSE 0) 

但此行是給我的錯誤:

selections.add(cb.sum(cb.<Number>selectCase().when(cb.equal(countryJoin.get("countryname"), country.getCountryname()), (Numeric)transaction.get("cfr")).otherwise(0))); 

Especifically這裏:

(Numeric)transaction.get("cfr") 

,因爲那可能」不會被鑄造成數字。 如何將Transaction.cfr發送到selectCase()?

這是整個方法:

private void generateSelect(FilterRegion filter) throws Exception 
    { 
     List<Country> countries = delegate.country().findAll(); 
     List<Selection<?>> selections = new LinkedList<>(); 

     if(filter.getContext() == FAMILY_CTX) 
     { 
      selections.add(familyJoin.get("prodfamname")); 
      cq.groupBy(familyJoin.get("prodfamname")); 
     } 
     else if(filter.getContext() == CUSTOMER_CTX){ 
      selections.add(customerJoin.get("customername")); 
      cq.groupBy(customerJoin.get("customername")); 
     } 
     else if(filter.getContext() == SUPPLIER_CTX){ 
      selections.add(supplierJoin.get("suppliername")); 
      cq.groupBy(supplierJoin.get("suppliername")); 
     } 

     for(Country country : countries) 
       selections.add(cb.sum(cb.<Number>selectCase().when(cb.equal(countryJoin.get("countryname"), country.getCountryname()), (Numeric)transaction.get("cfr")).otherwise(0))); 

     cq.multiselect(selections); 
    } 

回答

0

transaction.get( 「CFR」)作爲(Number.class)