无法提取结果集 当我在 Spring JPA 中使用本地查询

huangapple go评论78阅读模式
英文:

Could not extract ResultSet When i using Native Query in Spring Jpa

问题

我正试图使用Spring JPA本机查询获取带有比较条件的真或假。但我遇到了以下异常:

    无法提取结果集;SQL [n/a];嵌套异常是org.hibernate.exception.SQLGrammarException:无法提取结果集

我尝试的查询是:

    SELECT case when ABS(am_savedagent.AGENT_VERSION) < 1.8 then 'true' else 'false' end as bool 
    FROM am_savedagent where am_savedagent.BOX_ID="ots-JIO6Yn0jZbxs";

我的存储库:

    public interface SavedAgentRepository extends JpaRepository<SavedAgentDetails, String>, JpaSpecificationExecutor<SavedAgentDetails> {
        @Query(value = "SELECT case when ABS(SavedAgentDetails.agentVersion) < ?1 then 'true' else 'false' end as bool FROM SavedAgentDetails where SavedAgentDetails.boxId=?2", nativeQuery = true)
        public Optional<List<Object>> findByAgentVersionAndBoxId(String currentVersion, String boxid);
    }

我找不到错误为什么会出现。请有人帮帮我。
英文:

I am trying to get true or false, with some compare condition using Spring JPA native query. But I got following Exception,

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

The query I am trying is:

SELECT case when ABS(am_savedagent.AGENT_VERSION)&lt; 1.8 then &#39;true&#39; else &#39;false&#39; end as bool 
FROM am_savedagent where am_savedagent.BOX_ID=&quot;ots-JIO6Yn0jZbxs&quot;;

My Repository

public interface SavedAgentRepository extends JpaRepository&lt;SavedAgentDetails, String&gt;, JpaSpecificationExecutor&lt;SavedAgentDetails&gt; {
    @Query(value = &quot;SELECT case when ABS(SavedAgentDetails.agentVersion)&lt; ?1 then &#39;true&#39; else &#39;false&#39; end as bool FROM SavedAgentDetails where SavedAgentDetails.boxId=?2&quot;, nativeQuery = true)
    public Optional&lt;List&lt;Object&gt;&gt; findByAgentVersionAndBoxId(String currentVersion, String boxid);
}

I can't find y the Errors Comming. please help me, someone.

答案1

得分: 1

public interface RegisterAgentRepository extends JpaRepository<RegisterAgentDetails, String>, JpaSpecificationExecutor<RegisterAgentDetails> {
    @Query(value = "SELECT case when ABS(am_registeragent.AGENT_VERSION)< ?1 then 'true' else 'false' end as bool FROM am_registeragent where am_registeragent.SAASBOX_ID=?2", nativeQuery = true)
    public Optional<List<Object>> findByAgentVersionAndSaasboxId(String currentVersion, String saasboxid);
}
英文:

Since you are using native query =true,
that's why you need to use the table name and not the entity name inside your query.

Try this -->

public interface RegisterAgentRepository extends JpaRepository&lt;RegisterAgentDetails, String&gt;, JpaSpecificationExecutor&lt;RegisterAgentDetails&gt; {
    @Query(value = &quot;SELECT case when ABS(am_registeragent.AGENT_VERSION)&lt; ?1 then &#39;true&#39; else &#39;false&#39; end as bool FROM am_registeragent where am_registeragent.SAASBOX_ID=?2&quot;, nativeQuery = true)
    public Optional&lt;List&lt;Object&gt;&gt; findByAgentVersionAndSaasboxId(String currentVersion, String saasboxid);
}

huangapple
  • 本文由 发表于 2020年5月5日 01:24:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61598030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定