【问题标题】:In querydsl, how is a subquery performed in the FROM clause?在querydsl中,FROM子句中的子查询是如何执行的?
【发布时间】:2021-10-23 23:22:00
【问题描述】:

我有这个问题:

select gf_storage_zone_type,
       sum(numObjectPhysicalName) numObjects
from (
         select gf_storage_zone_type,
                count(distinct gf_object_physical_name) as numObjectPhysicalName
         from t_kgov_kpi_streaming_object
         group by gf_storage_zone_type,
                  gf_object_physical_name
         order by gf_storage_zone_type
     ) as v
group by gf_storage_zone_type

我正在尝试通过 Spring 使用 Java 中的 QueryDSL,其中我的一种方法包含:

NumberPath<Long> numObjectPhysicalName = Expressions.numberPath(Long.class, "numObjectPhysicalName");
NumberPath<Long> numObjects = Expressions.numberPath(Long.class, "numObjects");
QKpiRuleBoard qKpiRuleBoard = QKpiRuleBoard.kpiRuleBoard;
List<ObjectsByStorageZoneProjection> objectsByStorageZone = jpaQueryFactory
                .select(Projections.constructor(ObjectsByStorageZoneProjection.class,
                        qKpiRuleBoard.storageZoneType,
                        numObjectPhysicalName.sum().as(numObjects)))
                .from(JPAExpressions
                        .select(
                                Projections.constructor(ObjectsByStorageZoneProjection.class,
                                qKpiRuleBoard.storageZoneType,
                                qKpiRuleBoard.objectPhysicalName.countDistinct().count().as(numObjectPhysicalName)))
                        .from(qKpiRuleBoard)
                        .where(
                                qKpiRuleBoard.cutoffDate.eq(cutoffDate)
                                        .and(qKpiRuleBoard.countryId.eq(countryId))
                                        .and(qKpiRuleBoard.executionFrequencyType.eq(executionFrequencyType)))
                        .groupBy(
                                qKpiRuleBoard.storageZoneType,
                                qKpiRuleBoard.objectPhysicalName))
                .groupBy(qKpiRuleBoard.storageZoneType)
                .fetch();

抛出以下异常 -> com.querydsl.jpa.impl.JPAQuery 无法转换为 com.querydsl.core.types.EntityPath

我能做些什么来解决它?

【问题讨论】:

    标签: java mysql spring jpa querydsl


    【解决方案1】:

    通常您的查询将创建一个 inline view,因为在 from 子句中使用了子查询。

    QueryDSL 在后台与 JPA 配合使用,目前,它们不支持基于 docsfrom 子句中的子查询。

    将在以后的规范版本中考虑对 FROM 子句中子查询的支持。

    您必须重写查询以使其适应 JPA 或使用本机查询。

    我相信使用下面的查询你会得到同样的结果。

    select
        gf_storage_zone_type,
        count(distinct gf_object_physical_name) as numObjects
    from
        t_kgov_kpi_streaming_object
    group by
        gf_storage_zone_type
    order by
        gf_storage_zone_type;
    

    这可以很容易地转换为 JPA。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      相关资源
      最近更新 更多