Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ru.transaction.util; import java.math.BigDecimal; import java.util.Collection; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import ru.transaction.model.TransactionEntity; import ru.transaction.repository.JpaTransactionRepository; import ru.transaction.repository.TransactionRepository; /** * * @author andrey */ //@Service public class TransactionSumCalculatorRecursive implements TransactionCalculator { @Autowired TransactionRepository transactionRepository; @Override public BigDecimal calculateSum(BigDecimal currentSum, TransactionEntity transaction) { currentSum = currentSum.add(new BigDecimal(transaction.getAmount())); Collection<TransactionEntity> childTransactions = transactionRepository.findByParentId(transaction.getId()); for (TransactionEntity childTransaction : childTransactions) { currentSum = calculateSum(currentSum, childTransaction); } return currentSum; } }