6

I want to execute following Query using JPA repository implementation.

Query:select sum(amount) from transactions where transaction_date_time between '2017-05-04 00:01:07' and '2017-05-04 23:03:07' and id=2;

For finding I am using something like:

public interface TransactionRepository extends JpaRepository<Transaction, Serializable>{
Transaction findById(Long id);
}

So for sum I need something like this:

Double sumAmountByTransactionDateTimeBetweenAndMerchantId(Date from, Date to, Long merchantId);

I don't want to use @Query. I just want to create method name which will do my job. So, this is not a duplicate. I tried with above function name but it is not doing my job.

Vishal Jamdade
  • 182
  • 1
  • 2
  • 17
  • Where is a problem? – ByeBye Jun 19 '17 at 06:39
  • 1
    @scary-wombat this is not a duplicate... – ByeBye Jun 19 '17 at 06:41
  • @ByeBye Really, did you have a look at the link from the accepted answer? see http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query – Scary Wombat Jun 19 '17 at 06:43
  • @ByeBye If you still think so then please let me know why you think so – Scary Wombat Jun 19 '17 at 06:43
  • It seems that user want to create method name with given sql rather than use native query – ByeBye Jun 19 '17 at 06:47
  • @ByeBye The user can call his method whatever he wants. If he can do this without use `@Query` then please provide an answer – Scary Wombat Jun 19 '17 at 06:50
  • I tried to ask where is a problem because question is unclear, maybe better is wait until OP edit his question? – ByeBye Jun 19 '17 at 06:56
  • 3
    I don't think aggregation functions like `sum()` are supported by Spring Data JPA's query keywords: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords. So the best alternative is within the duplicates @ScaryWombat mentioned. And to be honest, wanting to do this without `@Query` isn't much of a requirement in my opinion. – g00glen00b Jun 19 '17 at 09:07

0 Answers0