我对ORM存取有些陌生,需要帮助理解一下。
假设我有以下标准SQL查询:
SELECT *, COUNT(test.testId) AS noTests FROM inspection
LEFT JOIN test ON inspection.inspId = test.inspId
GROUP BY inspection.inspId
我希望在JPA中使用它。
我有一个检查实体与一个测试实体之间的一对多关系。(一个检查有多个测试) 我尝试用JPQL编写了以下代码:
which I want to use in JPA.
I have an Inspection entity with a one-to-many relationship to a Test entity. (an inspection has many tests) I tried writing this in JPQL:
Query query = em.createQuery("SELECT insp, COUNT(???what???) " +
"FROM Inspection insp LEFT JOIN insp.testList " +
"GROUP BY insp.inspId");
1) 我应该如何编写 COUNT 子句?我需要对来自 test 表的元素应用 count,但是 testList 是一个集合,所以我无法像这样做 COUNT(insp.testList.testId)
2) 假设问题1已解决,返回的对象类型将绝对不是 Inspection 对象... 我应该如何使用结果?