【问题标题】:Hibernate HQL constructor expressionHibernate HQL 构造函数表达式
【发布时间】:2017-09-02 00:55:35
【问题描述】:

假设我有 2 个实体和那个查询:

select p, n.name
  from ProductOffering p
  left join ProductOfferingName n on (n.productOfferingId =     p.productOfferingId and n.salesChannelId= :salesChannelId)

Hibernate 将使用 p 中的所有列和 n 中的名称列发出 sql 查询,并返回我 Object[]。 但是处理 Object[] 不是类型安全的,所以我会尝试使用“构造函数表达式”:

select new com.peterservice.ccm.pom.internal.api.model.ProductOfferingResult(p, n.name)
  from ProductOffering p
  left join ProductOfferingName n on (n.productOfferingId = p.productOfferingId and n.salesChannelId= :salesChannelId)

使用该查询 Hibernate 仅选择 p.id + n.name 并为每一行发出“select * from ProductOffering p where p.id = :id”(n + 1 个问题)

这种行为是否正常且符合预期?

【问题讨论】:

    标签: hibernate hql


    【解决方案1】:

    尝试改为“left join fetch”:

    select new com.peterservice.ccm.pom.internal.api.model.ProductOfferingResult(p, n.name)
      from ProductOffering p
      left join fetch ProductOfferingName n on (n.productOfferingId = p.productOfferingId and n.salesChannelId= :salesChannelId)
    

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 1970-01-01
      • 2017-08-21
      • 2012-09-28
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 2017-11-21
      相关资源
      最近更新 更多