我有一个层次模型,我正在尝试构建一个由业务与其相关中心(一对多)组成的集合。以下是我使用的HQL:
SELECT new map(b.businessName as name, c as centers) FROM Business b JOIN b.centers c GROUP BY c.centerID
我的期望结果是(以JSON为参考):
[
{"name": "Business 1",
"centers": [ centerEntity1, centerEntity2, centerEntity... ]
},
{"name": "Business 2",
"centers": [ centerEntity3, centerEntity4, centerEntity... ]
}
]
然而,我的结果似乎更多地呈现一对一的关系:
[
{"name": "Business 1",
"centers": centerEntity1
},
{"name": "Business 2",
"centers": centerEntity2
}
]
此外,它正在运行1个查询以获取业务详细信息,然后进行其他查询以获取所有中心的信息。我认为这一切都可以使用JOIN FETCH组合成一个查询,但是会产生错误:
查询指定了联接获取,但被获取的关联的所有者不在选择列表中 [FromElement {explicit, not a collection join, fetch join, fetch non-lazy properties, classAlias = c, role = Business.centers, tableName = vw_hier_centers, tableAlias = centers1_, origin = vw_hier_businesses business0_,columns = {business0_.business_id, className = Center}}] [select new map(b.businessName as name, c as centers) FROM Business b JOIN FETCH b.centers c GROUP BY c.centerID]