I have this query in java-hibernate and I want to return this result. But returns null. How can I fix it?
BigDecimal totalbalance = null;
Query query = session.createQuery("select sum(beforeBalance) from DepositTransaction");
totalbalance = (BigDecimal) query;
return new BigDecimal(totalbalance+"");
Your HQL syntax is a bit off. Try this instead:
BigDecimal totalbalance = null;
String theQuery = "select sum(dt.beforeBalance) from DepositTransaction dt";
Query query = session.createQuery(theQuery);
totalbalance = (BigDecimal) query.getSingleResult();
return totalbalance;
This assumes that your DepositTransaction
class has a field called beforeBalance
with a getter method.