Spring的原生查询不适用于where子句

时间:2019-07-17 16:09:46

标签: mysql spring hibernate jpa nativequery

我有一个带有内部select语句的本机查询,该语句需要where
条件来自外部表的属性之一。在mySQL中运行查询时,查询工作正常,但在Spring中运行查询时,抛出SQLGrammerException。

01:38:24.429 [http-nio-8080-exec-1]错误o.h.e.jdbc.spi.SqlExceptionHelper-您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以在第1行的''附近使用正确的语法 01:38:24.444 [http-nio-8080-exec-1]错误oaccC [。[。[。[dispatcherServlet]-路径为[/ genchi]的Servlet [dispatcherServlet]的Servlet.service())引发了异常[Request处理失败;嵌套的异常是org.springframework.dao.InvalidDataAccessResourceUsageException:无法提取ResultSet。 SQL [n / a];嵌套的异常是org.hibernate.exception.SQLGrammarException:无法从根本原因中提取ResultSet] java.sql.SQLSyntaxErrorException:您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以在第1行的''附近使用正确的语法     在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)     在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)     在com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)     在com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:974)     在com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1024)     在com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)     在com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)     在org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)     在org.hibernate.loader.Loader.getResultSet(Loader.java:2167)     在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1930)     在org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1892)     在org.hibernate.loader.Loader.doQuery(Loader.java:937)     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)     在org.hibernate.loader.Loader.doList(Loader.java:2689)     在org.hibernate.loader.Loader.doList(Loader.java:2672)     在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506)     在org.hibernate.loader.Loader.list(Loader.java:2501)     在org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338)     在org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2223)     在org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1053)     在org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:170)     在org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)     在org.hibernate.query.Query.getResultList(Query.java:135)     在org.springframework.data.jpa.repository.query.JpaQueryExecution $ PagedExecution.count(JpaQueryExecution.java:201)     在org.springframework.data.jpa.repository.query.JpaQueryExecution $ PagedExecution.lambda $ doExecute $ 0(JpaQueryExecution.java:195)处     在org.springframework.data.repository.support.PageableExecutionUtils.getPage(PageableExecutionUtils.java:69)     在org.springframework.data.jpa.repository.query.JpaQueryExecution $ PagedExecution.doExecute(JpaQueryExecution.java:194

   @Query(value = "SELECT * FROM script_result SR LEFT JOIN script S ON 
   SR.script_id = S.ID "   
  + "INNER JOIN server_job_run SJR ON SR.server_job_run_id = SJR.id "  
  + "INNER JOIN suite_server_job SSJ ON SSJ.id = SJR.suite_server_job_id "
  + "LEFT JOIN suite ST ON ST.id = SSJ.suite_id "
  + "where (COALESCE(:project_id) is null or project_id IN (:project_id)) "
  + "AND (:suite_id is null or suite_id = :suite_id) "
  + "AND (:environment_name is null or E.name = :environment_name) "
  + "AND SJR.id = (SELECT inner_sjr.id FROM server_job_run inner_sjr "
  + "WHERE inner_sjr.suite_server_job_id = SSJ.id "
  + "ORDER BY inner_sjr.id DESC "
  + "LIMIT 1)"
  , nativeQuery = true)        


(SELECT inner_sjr.id FROM server_job_run inner_sjr "
  + "WHERE inner_sjr.suite_server_job_id = SSJ.id "
  + "ORDER BY inner_sjr.id DESC "
  + "LIMIT 1)" 

在这个内部选择中,当我给SSJ.id提供一些硬编码的值时,它可以工作,但不能正常工作

0 个答案:

没有答案