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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mohan
  • 1,164
  • 15
  • 18

1 Answers1

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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • I tried this also but still, it causes the same error. Thanks for your answer. – Mohan Oct 25 '19 at 08:28
  • 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. – Mohan Oct 25 '19 at 08:32
  • 1
    This is a general solution, can you confirm that you example is the same as your real code? – Youcef LAIDANI Oct 25 '19 at 08:49
  • Yes, this is my real code, I just copied it and pasted. please help me, I am struck with it. – Mohan Oct 25 '19 at 08:52
  • 1
    Can you please show me your code after edit ? And which version of spring boot you are using? – Youcef LAIDANI Oct 25 '19 at 08:59
  • My spring boot version: "2.1.8.RELEASE" and hibernate version: "5.3.11.Final" and STS version : "4.4.0.RELEASE" – Mohan Oct 25 '19 at 09:10
  • 1
    it should be public ```Object[][] getTestStats()``` – Emmanuel Ogoma Oct 25 '19 at 09:12
  • @MohanMunisifreddy please show me your code after edit – Youcef LAIDANI Oct 25 '19 at 09:14
  • @EmmanuelOgoma do you mean List will work? I tried it, but no use. – Mohan Oct 25 '19 at 09:15
  • @YCF_L @Query("SELECT new com.xxx.xxx.TestInstanceStats(t.testId, COUNT(t.id)) FROM TestInstance t GROUP BY t.testId") public List getTestStats(); ---> If I replace List with Object[] then it is working but I want to return it as TestInstanceStats – Mohan Oct 25 '19 at 09:22
  • @MohanMunisifreddy do you create a constructor in your class which how the id and Lond attribute as count? – Youcef LAIDANI Oct 25 '19 at 09:24
  • Ya, I created constructor with the same attributes. – Mohan Oct 25 '19 at 09:26
  • @YCF_L when I change the query to return only one record and map it to TestInstanceStats it is working i.e., public TestInstanceStats getTestStats(); but I am unable to return the list of projections – Mohan Oct 25 '19 at 09:30