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;
}
5
  • 1
    Maybe you are looking for Arrays. Commented Feb 28, 2017 at 10:32
  • Returning a map could help, so you can organize your objects in it Commented Feb 28, 2017 at 10:33
  • If it's always going to be 2 objects of that type, create another object to store them both and return that. Commented Feb 28, 2017 at 10:35
  • The types are not same, fdList - list and sum is not a list Commented Feb 28, 2017 at 10:40
  • Possible duplicate of How to return multiple objects from a Java method? Commented Apr 13, 2017 at 7:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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;
      }
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.