2

My question is related to this thread.

Following is my repository method using group by some field:

@Query(value = "SELECT  t.test_id AS testId, COUNT(t.id) AS total FROM test_instances t GROUP BY t.test_id", nativeQuery = true)
public Object[] getTestStats();

It's working and the result is obtained as follows:

[ [ 1, 2 ], [ 2, 1 ], [ 3, 2 ], [ 5, 1 ], [ 7, 2 ], [ 8, 1 ], [ 9, 1 ] ]

But, when I replace return type of getTestStats() from Object[] to List<?> I am getting the following error message:

{
"cause": null,
"message": "Couldn't find PersistentEntity for type class [Ljava.lang.Object;!"]
}

I want to use List<?> because if it is working, I want to use custom projection to cast it to i.e., List<CustomProjection>

I tried following return types {List<?>, List<CustomProjection>, CustomProjection[]}; but every thing is returning the same error. Hope someone will help me, thanks in advance.

1 Answer 1

2

If you want to return a List then :

  1. create a constructor which hold this fiels
  2. in your query you can create an Object which took this fields.

For example :

 Select new com.CustomObject(t.test_id, COUNT(t.id))

And in this case you can use List<CustomObject> instead of an array of objects

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

13 Comments

I tried this also but still, it causes the same error. Thanks for your answer.
If I return only one record from the query using where clause then I can easily map that record to the CustomObject, but I am unable to do it while returning multiple records.
This is a general solution, can you confirm that you example is the same as your real code?
Can you please show me your code after edit ? And which version of spring boot you are using?
it should be public Object[][] getTestStats()
|

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.