簡體   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