[英]How to create a Spring Data JPA query that will return objects matching 3 parameters from a list of objects of the same type
[英]How to return a List of Objects instead of Array of Objects from a Spring Data JPA GROUP BY query
我的問題與此線程有關。
以下是我使用按某個字段分組的存儲庫方法:
@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();
它正在工作,結果如下:
[ [ 1, 2 ], [ 2, 1 ], [ 3, 2 ], [ 5, 1 ], [ 7, 2 ], [ 8, 1 ], [ 9, 1 ] ]
但是,當我將 getTestStats() 的返回類型從Object[]
替換為List<?>
時,我收到以下錯誤消息:
{
"cause": null,
"message": "Couldn't find PersistentEntity for type class [Ljava.lang.Object;!"]
}
我想使用List<?>
因為如果它正在工作,我想使用自定義投影將其投射到即List<CustomProjection>
我嘗試了以下返回類型{List<?>, List<CustomProjection>, CustomProjection[]}
; 但每件事都返回相同的錯誤。 希望有人能幫助我,在此先感謝。
如果你想返回一個列表,那么:
例如:
Select new com.CustomObject(t.test_id, COUNT(t.id))
在這種情況下,您可以使用List<CustomObject>
而不是對象數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.