Search code examples
javaejbsumbigdecimalnamed-query

EJBQL Named Query - How to Sum BigDecimal


I have a BigDecimal representing money. I want to be able to select a sum of it in a NamedQuery but it won't sum BigDecimals.

@NamedQuery(name=Account.CALCULATE_TRANSACTIONS, query="SELECT SUM(a.credit) + SUM(a.debit) FROM Transaction a WHERE a.account.id = :accountId AND a.journalEntry.staged = false")

where credit and debit are BigDecimals. This doesn't work. What would be the best way to do this besides pulling them all out, iterating them and generating a total. I know I can do that but looking for a way to do it in one query if possible.

Thanks!


Solution

  • Just an update/answer. From everything I have researched, this just isn't possible in EJBQL3. I had to do it outside the scope of the named queries.