繁体   English   中英

下面的 JPQL 查询有什么问题?

[英]What is wrong or incorrect in the below JPQL query?

下面是我写的 JPQL 查询,但它不起作用。 我收到以下错误:NoViableAltException:意外令牌:最大值

有人可以指出以下查询中的错误吗?

SELECT new com.chp.cef.api.dto.EventSearchDTO(ec.eventName, e.eventKey, e.eventTime, CAST(e.messagePayload AS string), epl.status, epl.statusReason, CAST(epl.processLogPayload AS string), ec.source, " +
            "epl.eventProcessLogKey, epl.eventReceivedTime, epl.eventProcessedTime) FROM Event e " +
            "left join (SELECT inEpl.eventKey, max(inEpl.eventReceivedTime), inEpl.eventProcessLogKey, inEpl.status, inEpl.statusReason, inEpl.processLogPayload, inEpl.eventProcessedTime" +
            "FROM EventProcessLog inEpl GROUP BY inEpl.eventKey) epl ON e.eventKey = epl.eventKey " +
            "left join EventConfiguration ec ON e.eventConfigKey = ec.eventConfigurationKey WHERE ec.eventName = coalesce(:eventName, ec.eventName) " +
            "AND epl.status = coalesce(:eventStatus, epl.status) AND e.eventTime >= :eventStartTime AND e.eventTime <= :eventEndTime

您不能在 JPQL/HQL 中加入子查询。 您必须将其重新表述为 ON 子句中的相关子查询。

话虽如此,我认为这是Blaze-Persistence Entity Views的完美用例,并且它支持通过@Limit注释获取每个类别的 TOP-N。

我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义的模型之间轻松映射,例如类固醇上的 Spring Data Projections。 这个想法是您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。

但是 Blaze-Persistence 不仅可以帮助您进行 DTO 映射,还可以使用一些更高级的 SQL 概念,在本例中为横向连接。

使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:

@EntityView(Event.class)
public interface EventSearchDTO {
    @IdMapping
    String getEventKey();
    String getEventName();
    Instant getEventTime();
    @Mapping("CAST_STRING(messagePayload)")
    String getMessagePayload();
    // ...

    @Limit(limit = "1", order = "eventReceivedTime DESC")
    @Mapping("EventProcessLog[eventKey = VIEW(eventKey)]")
    // If you have an association mapping, can be simplified to
    // @Mapping("processLogs")
    EventProcessLogDto getLatestLog();

    @EntityView(EventProcessLog.class)
    interface EventProcessLogDto {
        @IdMapping
        String getEventKey();
        String getEventProcessLogKey();
        // ...
    }

    @Mapping("EventConfiguration[eventConfigurationKey = VIEW(eventConfigKey)]")
    EventConfigurationDto getConfiguration();

    @EntityView(EventConfiguration.class)
    interface EventConfigurationDto {
        @IdMapping
        String getEventConfigurationKey();
        // ...
    }
}

查询是将实体视图应用于查询的问题,最简单的只是按 id 查询。

EventSearchDTO a = entityViewManager.find(entityManager, EventSearchDTO.class, id);

Spring Data 集成允许您像使用 Spring Data Projections 一样使用它: https : //persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

这将生成类似于以下内容的 SQL 查询

SELECT ec.eventName, ... 
FROM Event e
left join lateral (SELECT * FROM EventProcessLog inEpl WHERE e.eventKey = inEpl.eventKey ORDER BY inEpl.eventReceivedTime DESC LIMIT 1) epl ON 1=1
left join EventConfiguration ec ON e.eventConfigKey = ec.eventConfigurationKey

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM