title: Spring Data Hibernate JPQL Aggregate Query
body: <p>I have a Spring Data implementation with an entity </p>
  
  <pre><code>@Entity
  public class Account extends AbstractEntity {
  
  ...
  
      @OneToMany(mappedBy = "account")
      private Set&lt;AccountTransaction&gt; accountTransactions;
  </code></pre>
  
  <p>And the <code>AccountTransactions</code></p>
  
  <pre><code>@Entity(name="AccountTransactions")
  public class AccountTransaction extends AbstractEntity {
  
      /**
       * 
       */
      private static final long serialVersionUID = -7045838402463741959L;
  
      @ManyToOne(optional = false)
      @JsonIgnore
      private Account account;
  
      @Column
      @Enumerated(EnumType.ORDINAL)
      private TransactionType transactionType;
  
      @Column
      private BigDecimal amount;
  </code></pre>
  
  <p>And my repository, where I'm trying to execute an aggregate query: <code>sum(amount)</code></p>
  
  <p>The result is <code>null</code> however</p>
  
  <pre><code>public interface AccountTransactionRepository extends
          CrudRepository&lt;AccountTransaction, Long&gt; {
  
      // @Query(value =
      // "select sum(amount) from AccountTransaction where account.id = ?1")
      @Query(value = "SELECT sum(at.amount) FROM AccountTransactions at WHERE at.account.id = :accountId")
      public BigDecimal getAccountBalance(@Param("accountId") Long accountId);
  }
  </code></pre>
  
  <p>What do I need to do to get the <code>BigDecimal</code> sum of the column <code>amount</code>?</p>
  
  <p>*<strong><em>UPDATE</em>*</strong></p>
  
  <p>I was able to get the query to work changing it to a nativeQuery like so:</p>
  
  <pre><code>@Query(value = "SELECT sum(amount) FROM account_transaction WHERE account_id = :accountId", nativeQuery = true)
      public BigDecimal getAccountBalance(@Param("accountId") Long accountId);
  </code></pre>
  
  <p>This is obviously less than desirable ... as I don't want to have to write this for every query ... </p>