将SQL转换为JPQL:使用SELECT

时间:2019-04-02 12:30:47

标签: java sql spring-boot jpa jpql

我有以下SQL语句,用于对Spring Boot后端进行非常复杂的搜索。不需要,我需要将其转移到JQPL以便在我的存储库中进行搜索。

SELECT DISTINCT dav_n.*
  FROM [DwhTest].[dbo].[DimensionAttributeValue] dav_n
  INNER JOIN [DwhTest].[dbo].[DimensionAttributeValue] dav_l on dav_n.MasterData_ID = dav_l.MasterData_ID AND dav_l.DimensionAttribute_ID = $PARAM1 AND dav_l.LstValue = $PARAMA2
        AND ((
            SELECT TOP 1 StartDate FROM DimensionAttributeValue
            WHERE MasterData_ID = dav_n.MasterData_ID AND DimensionAttribute_ID = dav_n.DimensionAttribute_ID AND StartDate <= getdate()
            ORDER BY StartDate DESC
        ) = dav_n.StartDate OR dav_n.StartDate IS NULL)
  WHERE dav_n.DimensionAttribute_ID = $PARAM3 AND dav_n.ChrValue LIKE '$PARAM4%'

$PARAM1-$PARAM4是我的搜索变量。以下清单是我的PagingAndSortingRepository的代码:

@Query(value = "SELECT DISTINCT dav_n FROM DimensionAttributeValue dav_n " +
            "INNER JOIN DimensionAttributeValue dav_l on dav_n.MasterData_ID = dav_l.MasterData_ID AND dav_l.DimensionAttribute_ID = :lstAttributeId AND dav_l.LstValue = :lstValue " +
            "AND ((SELECT TOP 1 StartDate FROM DimensionAttributeValue WHERE MasterData_ID = dav_n.MasterData_ID AND DimensionAttribute_ID = dav_n.DimensionAttribute_ID AND StartDate <= getdate() ORDER BY StartDate DESC) = dav_n.StartDate OR dav_n.StartDate IS NULL)" +
            "WHERE dav_n.DimensionAttribute_ID = :chrValueAttributeId AND dav_n.ChrValue LIKE ':chrValue%'")
    Page<DimensionAttributeValue> searchByCharValueAndFilter(
            @Param(value = "chrValueAttributeId") Long chrValueAttributeId, @Param(value = "lstAttributeId") Long lstAttributeId,
            @Param(value = "chrValue") String chrValue, @Param(value = "lstValue") Long lstValue, Pageable pageable);

AND (([..]语句出现错误:'(', <expression>, FUNCTION or identifier expected, got '('

有人可以帮助我翻译此声明吗?谢谢!

1 个答案:

答案 0 :(得分:0)

在这些括号内,您有一个g.drawLine(x, y, width, height); 和比较器的一侧。

您需要将结果与某些内容进行比较或添加存在的内容:

AND