【问题标题】:HQL Map with relationships具有关系的 HQL 映射
【发布时间】:2015-03-27 12:33:42
【问题描述】:

我有一个层次结构模型,我试图在其中建立一系列企业及其关联的中心(一对多)。我正在使用的 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 组合成 1 个查询,但这会产生错误:

查询指定了连接提取,但提取关联的所有者不在选择列表中 [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 centres) FROM Business b JOIN FETCH b.centers c GROUP BY c.centerID]

【问题讨论】:

    标签: hibernate coldfusion hql coldfusion-10


    【解决方案1】:

    这背后的想法太复杂了。这是完全符合我需要的解决方案:

    SELECT DISTINCT b FROM Business b JOIN FETCH b.centers
    

    1 次查询,它会获得一系列企业及其获取的中心。

    【讨论】:

      猜你喜欢
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多