0

I have a resultList which fetches result from a JPQL query that queries multiple tables as described :

Query query = em.createQuery("SELECT protein.gid,protein.uniProtAccession,protein.name,protein.ECNumber,ttdtarget.uniProtID,ttdtarget.gid FROM Protein protein,TtdTarget ttdtarget WHERE protein.uniProtAccession = ttdtarget.uniProtID");

List resultList = query.getResultList();

Note: I am restricting the size of resultset to 5 right now, just for debugging. I want to get the values returned inside each object from the resultList, which basically is an array of objects.

So far I have tried iterating upto the objects but can't access the inner values.

for (int i = 0; i < resultList.size(); i++)
{
System.out.println("->"+resultList.get(i));         
}

Output:

->[Ljava.lang.Object;@141ab9e
->[Ljava.lang.Object;@6a15ca
->[Ljava.lang.Object;@bcb654
->[Ljava.lang.Object;@1664b54
->[Ljava.lang.Object;@db953c

And here is the variable's output from debug:

enter image description here

So my question is how to access those values inside the object.

6 Answers 6

3

The result is List<Object[]>, so cast to that. So a list, where each element is an array of values. You must then cast each value to its type (which you know beforehand).

If you simply want to iterate and print:

List<Object[]> resultList = (List<Object[]>) query.getResultList();
for (Object[] array : resultList) {
   for (Object field : array) {
      System.out.println("->"+field); 
   }
}

Alternatively, you can create a new class which has these exact fields, make its constructor accept all of the values, and use it in the query: SELECT new Foo(.....) FROM... There you can use the generic alternative of em.createQuery(..) that returns TypedQuery

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

2 Comments

You may not use the generic version of createQuery() for this kind of query. The javadoc says: "The select list of the query must contain only a single item, which must be assignable to the type specified by the resultClass argument."
thanks, I'll add it to the "alternative" (I started from that option in mind, and then messed up the order :) )
3

You don't have a List of Objects, it is a List of Objectarrys

List<Object[]> resultList = (List<Object[])resultList;
for (int i = 0; i < resultList.size(); i++)
{
  System.out.print("protein.gid ->"+resultList.get(i)[0]);
  System.out.println("protein.uniProtAccession ->"+resultList.get(i)[1]);        
}

2 Comments

Well this part is giving error : resultList.get(i)[0]), can't access as array.
Sorry i forgott the cast
2
List<Object[]> resultList = query.getResultList();
for (Object[] objects : resultList)
{
       for (Object object : objects)
       {
          System.out.println(object)
       }
}

Comments

1

Your result form the query is array of objects.

List resultList = query.getResultList();

for(Object result : resultList) {
  Object[] results = (Object[]) result;
   for(Object res : results) {
     System.out.println(res);
   }
}

Or you can go with Bozho solution and create new direct from query.

Comments

0

You can create a new class with the exact fields you need and use a TypedQuery

TypedQuery<CustomClass> query = em.createQuery("" /* Query String */, CustomClass.class);
List<CustomClass> resultList = query.getResultList();
foreach (CustomClass result : resultList){
    // Need to override method toString() in CustomClass
    System.out.println("->" + result);         
}

Comments

-1

I'm tempted to suggest nesting a for loop:

for (int i = 0; i < resultList.size(); i++)
{
  for(int j = 0; j < resutList.get(i).size(); j++){
    System.out.println("->"+resultList.get(i).get(j));
  }         
}

1 Comment

It's a List<Object[]>, not a List<List<Object>>.

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.