create hibernate query to perform sum of two select query columns

I have two select statements which calculates the sum of a column each .Need to fetch the sum of the two select statements . Its working in normal sql but not working on @Query annotation of hibernate.

Tried running the above problem using normal sql query its working but the same is not working on hibernate @Query annotation


select sum((SELECT   SUM(TRAN_AMT+FEE_AMT)
            FROM pymt 
            WHERE TRANTM between '2019-06-01 00:00:00' and  '2019-06-23 23:59:59'
            AND PAYEE = unhex('xxxxxx')
            AND TRANTYPECDE = '003' 
            AND RESP_CDE = 'Success')-(

SELECT   SUM(TRAN_AMT-FEE_AMT)
            FROM pymt
            WHERE TRAN_CAPTR_DT_TM between '2019-06-01 00:00:00' and  '2019-06-23 23:59:59'
            AND PAYEE = unhex('xxxxxx')
            AND TRANTYPECDE != '003' 
            AND RESP_CDE = 'Success')) as sumValue;   

above is working but the below query in hibernate not working

    @Query(value = "SELECT SUM((SELECT   SUM(p.feeAmt + p.tranAmt) "
                    +"FROM PymtEntity p " 
                    + "WHERE p.tranCaptrTm >= :from AND p.tranCaptrTm <= :to "
                    +"AND p.tranTypeCde = '003' " 
                    + "AND p.payeeId = :md "
                    + "AND p.respCde = :re )"
                    +"-"
                    +"(SELECT   SUM( p.tranAmt - p.feeAmt) "
                    +"FROM PymtEntity p " 
                    + "WHERE p.tranCaptrTm >= :from AND p.tranCaptrTm <= :to "
                    +"AND p.tranTypeCde != '003' " 
                    + "AND p.payeeId = :md "
                    + "AND p.respCde = :re )) as sumtotal")

above return QuerySyntaxException: unexpected end of subtree

expect the sum to be printed
but getting QuerySyntaxException: unexpected end of subtree in hibernate

Stacktrace

    ... 29 common frames omitted
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [SELECT SUM((SELECT   SUM(p.feeAmt + p.tranAmt) FROM com.hsbc.digital.peak.payments.reporting.data.entities.PymtEntity  p WHERE p.tranCaptrTm >= :from AND p.tranCaptrTm <= :to AND p.tranTypeCde = '003' AND p.payeeId = :merchantId AND p.respCde = :respCode )-(SELECT   SUM( p.tranAmt - p.feeAmt) FROM com.hsbc.digital.peak.payments.reporting.data.entities.PymtEntity  p WHERE p.tranCaptrTm >= :from AND p.tranCaptrTm <= :to AND p.tranTypeCde != '003' AND p.payeeId = :merchantId AND p.respCde = :respCode )) as sumtotal]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:713)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)
    at com.sun.proxy.$Proxy135.createQuery(Unknown Source)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87)
    ... 58 common frames omitted
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree [SELECT SUM((SELECT   SUM(p.feeAmt + p.tranAmt) FROM com.hsbc.digital.peak.payments.reporting.data.entities.PymtEntity  p WHERE p.tranCaptrTm >= :from AND p.tranCaptrTm <= :to AND p.tranTypeCde = '003' AND p.payeeId = :merchantId AND p.respCde = :respCode )-(SELECT   SUM( p.tranAmt - p.feeAmt) FROM com.hsbc.digital.peak.payments.reporting.data.entities.PymtEntity  p WHERE p.tranCaptrTm >= :from AND p.tranCaptrTm <= :to AND p.tranTypeCde != '003' AND p.payeeId = :merchantId AND p.respCde = :respCode )) as sumtotal]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
    at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:277)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:595)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:704)
    ... 66 common frames omitted

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

I’m biased since I develop it, yet this can be solved with FluentJPA since it feeds native SQL to Hibernate: @Entity @Data // lombok public static class PymtEntity { @Id private int id; private int feeAmt; private int tranAmt; private Timestamp tranCaptrTm; private String tranTypeCde; private int payeeId; private String respCde; } public int sumTwoColumns(Date from, Date to, int md, String re) { FluentQuery query = FluentJPA.SQL(() -> { PymtEntity common = subQuery((PymtEntity pymt) -> { SELECT(pymt); FROM(pymt); WHERE(BETWEEN(pymt.getTranCaptrTm(), from, to) && pymt.getPayeeId() == md && pymt.getRespCde() == re); }); PymtEntity first = subQuery(() -> { SELECT(common); FROM(common); WHERE(common.getTranTypeCde() ==… Read more »