0
Query  w=em.createQuery("SELECT ar.authorId.firstName,
                        count(ar) FROM Article ar
                        WHERE ar.categoryId.categoryText=:
                                                 categoryText GROUP BY ar.authorId"); 
       w.setParameter("categoryText", categoryText); 

       List list1=w.getResultList();  


       System.out.println(list1.toString());

Whats the wrong. i get [[Ljava.lang.Object;@60a19573, [Ljava.lang.Object;@44a085e5]

I want to print the query like "name:"+firtname+"Articles:"+count(ar)

4
  • 1
    cast to specific type of List and then implement toString() in that particular type Commented Dec 30, 2014 at 22:33
  • First of all it would be nice to give your colleagues a hint what the objects are: hibernate??? Commented Dec 30, 2014 at 22:33
  • possible duplicate of how to convert object to string in java Commented Dec 30, 2014 at 22:33
  • By default the toString method get the class names. You should have a method like myList.Cast<String>().ToList(), that is how we do in C#. Commented Dec 30, 2014 at 22:35

3 Answers 3

4

Implement toString() in that particular type SomeType

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

5 Comments

The generic cast isn't necessary just for nice output; toString() is necessary and sufficient.
You could make it a little clearer that the last line of your answer is the only one that matters. If it were me, I'd just delete the first six lines.
What is SomeType here?
the object model type class that you are querying from DB
please could you be more specific how to do this.
2

What you're getting back in your query is a List<Object[]> which is expected as per the spec. The first element in your array is the string ar.authorId.firstName and the second is a Long representing count(ar)

A String.format should work here for you:

for(Object[] objs : list1) {
    System.out.println(String.format("name %s articles %s",objs[0],objs[1]);
}

1 Comment

If you're satisfied with the answer @palios7 please do accept it :-)
0

for(Object objs : list1) { System.out.println(String.valueOf(objs)); }

valueOf is an String method which helps to convert primitive data types to String.

1 Comment

it doesnt work i get the same [Ljava.lang.Object;@60a19573, [Ljava.lang.Object;@44a085e5]

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.