How to use ISNULL in jpa

I have a table test1 with a number of columns. I want to get the sum of all columns. So I am using a SUM function like this

SELECT SUM(cda.amount)FROM test cda

      

If the table is empty then it gives me null

The equivalent JPA code is as follows

If there is n

public Double sumAmount()
    {
        Query query=entityManagerUtil.getQuery("SELECT SUM(cda.amount)FROM test cda");

        Object result=query.getSingleResult();
        return (Double)result;
    }

      

Now in my controller, I add this amount to the main one;

Double total=prinicipal+daoImpl.sumAmount();

      

As sumAmount()

returns null

, so I get NULLPOINTEREXCEPTION when adding hereprinicipal+daoImpl.sumAmount();

So I am thinking to return 0 if the sum is zero, so tried ISNULL and IFNULL like this

SELECT ISNULL(SUM(cda.amount),0) FROM test cda

      

but here it is giving me the following error

No data type for node: org.hibernate.hql.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'ISNULL' {originalText=ISNULL}

      

So can someone please tell me how to use ISNULL correctly in JPA

+3


source to share


1 answer


The coalesce command is used as the equivalent of ISNUL or NVL . so you can useSELECT coalesce(SUM(cda.amount),0) FROM test cda



+11


source







All Articles