0

In here I'm trying to return two results as one response and not sure what option may good with it. In here it return as a list,

List<FixedDeposit> fdList = em.createQuery(sb.toString())
            .setParameter("start", dateFrom)
            .setParameter("end", dateTo)
            .setFirstResult(start).setMaxResults(limit)
            .getResultList();

so I have got it as

List<FDSearchResult> searchList = getSearchResult(fdList); 
response.setData(searchList);
return response;

And now again I want to return another response. And this is the calculation,

Double sum = 0.0;
sum = (Double) em.createQuery("SELECT SUM(deposit_amount)FROM fd_fixed_deposit where fd.status in ('ACT','REN','WDR') and fd.maturityDate between :start and :end ")
                .setParameter("start", dateFrom)
                .setParameter("end", dateTo).getSingleResult();

So how can I return the result through response for the sum? Please Help

I will post the full method,

public Response<List<FDSearchResult>> loadMaturedFDByRange(String fromDate, String toDate, final Integer start,
        final Integer limit) {
    final Response<List<FDSearchResult>> response = new Response<List<FDSearchResult>>();
    Date dateFrom = DateUtils.parseDate("dd/MM/yyyy", fromDate, null);
    Date dateTo = DateUtils.parseDate("dd/MM/yyyy", toDate, null);
    Long count = 0L;
    Double sum = 0.0;
    try {
        count = (Long) em.createQuery("SELECT COUNT(fd) FROM FixedDeposit fd where fd.status in ('ACT','REN','WDR') "
                + "and fd.maturityDate between :start and :end ")
                .setParameter("start", dateFrom)
                .setParameter("end", dateTo).getSingleResult();
    } catch (NoResultException nre) {
        count = 0L;
    }
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT fd FROM FixedDeposit fd where fd.status in ('ACT','REN','WDR') and fd.maturityDate between :start and :end ");
    List<FixedDeposit> fdList = em.createQuery(sb.toString())
            .setParameter("start", dateFrom)
            .setParameter("end", dateTo)
            .setFirstResult(start).setMaxResults(limit)
            .getResultList();

    sum = (Double) em.createQuery("SELECT SUM(deposit_amount)FROM fd_fixed_deposit where fd.status in ('ACT','REN','WDR') and fd.maturityDate between :start and :end ")
                .setParameter("start", dateFrom)
                .setParameter("end", dateTo).getSingleResult();

    List<FDSearchResult> searchList = getSearchResult(fdList);
    response.setData(searchList);
    response.setPagination(new Response().new PaginationInfo(count));
    return response;
}
Shantha
  • 13
  • 2
  • 6

3 Answers3

3

In this instance, I would create an Object Container that holds these two objects and return that object instead.

public class MyContainer
{
   List<FixedDeposit> fdList;
   List<FDSearchResult> searchList;

   public MyContainer()
   {

   }
}

This is how I would approach this.

1

You can return all relevant data via result objects. Just create a new class which can obtain all your needed values.

kamehl23
  • 522
  • 3
  • 6
0

Two options:

  1. HashMap. Type cast Object to response and Double respectively. This is not a good practice

  2. You can create a custom class which can encapsulate different object types. Generics is what you need here.

    public class MultiReturn<T> {
      public final String name;
      public final T object;
    
      public NamedObject(String name, T object) {
         this.name = name;
         this.object = object;
      }
    }
    
uday
  • 361
  • 1
  • 11