无法将SQL转换为JPQL

时间:2019-05-14 06:01:51

标签: sql spring oracle jpql

我正在尝试转换以下Oracle SQL(有效)

select sum(ct.some_count) from TABLE1 mc
  inner join TABLE2 xref on mc.cnum = xref.cnum
  inner join TABLE3 ct on xref.srt = ct.srt
  inner join TABLE4 pc on pc.id = xref.id
  where mc.CARD_NO = '111' and pc.code = '222';

到遵循JPQL。

@Query("SELECT sum(ct.someCount) FROM Table1Entity mc " +
        "inner join TABLE2Entity xref on mc.cnum = xref.cnum " +
        "inner join TABLE3Entity ct on xref.srt = ct.srt " +
        "inner join TABLE4Entity pc on pc.id = xref.id " +
        "where mc.CARD_NO = :cardNumber and pc.code = :code")
long getTotalCount(@Param("cardNumber") String cardNumber, @Param("code") String code);

我收到以下异常。

  

QuerySyntaxException:意外令牌

请问我做错了什么吗?

我正在使用Spring,这是此查询的存储库。

@Repository(value = "someCountRepository")
public interface SomeCountRepository extends JpaRepository<Table3Entity, TableId> {
        @Query("SELECT sum(ct.someCount) FROM Table1Entity mc " +
        "inner join TABLE2Entity xref on mc.cnum = xref.cnum " +
        "inner join TABLE3Entity ct on xref.srt = ct.srt " +
        "inner join TABLE4Entity pc on pc.id = xref.id " +
        "where mc.CARD_NO = :cardNumber and pc.code = :code")
        long getTotalCount(@Param("cardNumber") String cardNumber, @Param("code") String code);
}

1 个答案:

答案 0 :(得分:1)

@Query("SELECT sum(ct.someCount) FROM Table1Entity mc " +
        "inner join TABLE2Entity xref on mc.cnum = xref.cnum " +
        "inner join TABLE3Entity ct on xref.srt = ct.srt " +
        "inner join TABLE4Entity pc on pc.id = xref.id " +
        "where mc.CARD_NO = :cardNumber and pc.code = :code",
        nativeQuery = true) // set native query to true
long getTotalCount(@Param("cardNumber") String cardNumber, @Param("code") String code);

由于要完全从不是JPQL的Oracle SQL复制查询,因此应将其声明为本地查询。

参考文献:Spring Data JPA @Query#2.2。原生