@Query(Spring data / JPA),其中boolean在where条件中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@Query(Spring data / JPA),其中boolean在where条件中相关的知识,希望对你有一定的参考价值。

SampleRepository.java类有如下查询:

@Query("select eq from EmailQueue eq where eq.inquiry in (select issue.id from Inquiry issue  where issue.id = eq.inquiry and systimestamp   >= eq.sendDate + (1/24) * ?3  ) and "
            + " systimestamp   >= eq.sendDate + (1/24) * ?2 and eq.sent = True and "            
            + " eq.emailType.id = ?1  "
            + "  order by eq.sendDate asc")

我得到的错误是:

Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "TRUE": invalid identifier

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:587) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]

你能告诉我如何在@query中指定eq.sent = True吗?我也尝试了eq.sent = 1,它不能正常工作。

答案

您可以使用is而不是=

@Query("select eq from EmailQueue eq where eq.inquiry in (select issue.id from Inquiry issue  where issue.id = eq.inquiry and systimestamp   >= eq.sendDate + (1/24) * ?3  ) and "
            + " systimestamp   >= eq.sendDate + (1/24) * ?2 and eq.sent is True and "            
            + " eq.emailType.id = ?1  "
            + "  order by eq.sendDate asc")
另一答案

通过以下解决方法解决:

 @Query("select eq from EmailQueue eq where eq.inquiry in (select issue.id from Inquiry issue  where issue.id = eq.inquiry and systimestamp   >= eq.sendDate + (1/24) * ?3  ) and "
                + " systimestamp   >= eq.sendDate + (1/24) * ?2 and eq.sent = ?4 and "          
                + " eq.emailType.id = ?1  "
                + "  order by eq.sendDate asc")
List<EQueue> findEmailSentDetails(long type, int days1, int days2,boolean emailSent);

而不是使用“eq.sent = true”传递true作为参数,如eq.sent =?4

以上是关于@Query(Spring data / JPA),其中boolean在where条件中的主要内容,如果未能解决你的问题,请参考以下文章

spring data jpa @query的用法

@Query注解的用法(Spring Data JPA)

@Query注解的用法(Spring Data JPA)

spring-data-jpa 的@Query注解的使用

Spring Data JPA Update @Query 没有更新?

Spring data jpa中Query和@Query分别返回map结果集