CriteriaBuilder - amount using SelectCase

I am trying to execute a SQL summarization query as follows:

select group_ID, sum(case when user_type = 'Exec' then 1000  
                          when user_type = 'Office' then 10 else 0 end)  
from subscription  
group by group_ID;  

using the following code snippet from a CriteriaBuilder request:

criteriaBuilder.sum(
  criteriaBuilder.selectCase()  
     .when(criteriaBuilder.equal(subscriptionJoin.get(Subscription_.userType), "Exec"),1000)  
     .when(criteriaBuilder.equal(subscriptionJoin.get(Subscription_.userType), "Office"),1)  
     .otherwise(101))  

However, the following compilation error appears:

The deduced type 'java.lang.object' for a parameter of type 'N' is not within its boundary; should extend 'java.lang.number'

Any idea how to support summation execution using selectCase?

+5
source share
2 answers

The amount is determined as follows:

<N extends Number> Expression<N> sum(Expression<N> x);

Therefore, the cause of the compilation error is that the sum method expects arguments that are an expression with a type that extends Number. It determines the type from selectCase and ends with java.lang.Object, which is unacceptable.

(<Number>):

criteriaBuilder.sum(
  criteriaBuilder.<Number>selectCase()
+7

Spring Data JPA , , . " ", .

, , .

    public interface ITransactionEntryRepo extends PagingAndSortingRepository<TransactionEntryEntity, String> {

        @Query("select SUM(CASE WHEN te.debit = 'Y' THEN (te.amount * - 1) WHEN te.debit = 'N' THEN te.amount ELSE 0 END) AS availablebalance FROM TransactionEntity t, TransactionEntryEntity te WHERE t.id = te.transactionEntity.id and te.accountEntity.id = :id and te.valid = 'T' and t.retcode = 'XX' GROUP BY te.accountEntity.id")
            public double findAvailableBalance(@Param("id") String id);
}

,

double balance = iTransactionEntryRepo.findAvailableBalance(accountEntity.getId());

(), . , -.

+1

All Articles