1

I have code like this...

Query query = em.createQuery("select CM.salesAreaId, SM.salesAreaDesc, count(CM.salesAreaId), sum(CM.openBal), " +
                    "sum(CM.netSales) from CustomerMaster CM, SalesAreaMaster SM where CM.salesAreaId=SM.salesAreaCode and CM.companyId=SM.companyId" +
                    " and CM.companyId=:companyId group by CM.salesAreaId, SM.salesAreaDesc");
query.setParameter("companyId", companyId);
List list = query.getResultList();

From above code how can i get the list values?(list.get() values prints objects)

suresh manda
  • 659
  • 1
  • 8
  • 25

3 Answers3

2

Try this

Query query = em.createQuery("select CM.salesAreaId, SM.salesAreaDesc, count(CM.salesAreaId), sum(CM.openBal), " +
                    "sum(CM.netSales) from CustomerMaster CM, SalesAreaMaster SM where CM.salesAreaId=SM.salesAreaCode and CM.companyId=SM.companyId" +
                    " and CM.companyId=:companyId group by CM.salesAreaId, SM.salesAreaDesc");
query.setParameter("companyId", companyId);
List list = query.getResultList();
for(Object o : list) {
    Object[] obj = (Object[])o;

}

And the value of CM.salesAreaId should be in obj[0]

ccheneson
  • 49,072
  • 8
  • 63
  • 68
1

You could use a for loop to iterate through, providing the index each time.

for (int i = 0; i < list.size(); i++)
{
    Object x = list.get(i);
    ...
}

http://docs.oracle.com/javase/6/docs/api/java/util/List.html#get(int)

clicky
  • 865
  • 2
  • 14
  • 31
1

as JPA is an ORM spec, query result will necessary be a set of Objects. There is a workaround with hibernate if you're using this last as JPA implementation (see How to fetch hibernate query result as associative array of list or hashmap) but your code will be heavily coupled to hibernate then.
If you want to retrieve result as a resultSet (set of primitive values) you should use jdbc directly.

If your problematic is just to print the result in an human readable way you should override toString() method for all your JPA entities.

Community
  • 1
  • 1
Gab
  • 7,869
  • 4
  • 37
  • 68