使用 JPA 的原生查询在对象列表数组中返回多列数据

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

use jpa nativequery multiple columns in object list array

问题

List<Object[]> queryList = new ArrayList<>();
String[] arr = {"val1", "val2"};
queryList.add(arr);

String sql = "SELECT * FROM TABLE A WHERE (A.COL1, A.COL2) IN (:queryList)";

Query query = entityManager.createNativeQuery(sql);

query.setParameter("queryList", queryList);
-- In postgresql like this
SELECT * FROM TABLE A WHERE (A.COL1, A.COL2) IN (('val1', 'val2'), ('val3', 'val4'));
-- Here is the Exception
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: record = bytea
建议:No operator matches the given name and argument types. You might need to add explicit type
Is this possible?
英文:

>Use jpa nativequery multiple columns in object list array

List&lt;Object []&gt; queryList = new ArrayList&lt;&gt;();
String[] arr = {&quot;val1&quot;, &quot;val2&quot;};
queryList.add(arr);

String sql = SELECT * FROM TABLE A WHERE (A.COL1, A.COL2) IN (:queryList)

Query query = entityManager.createNativeQuery(sql);

query.setParameter(&quot;queryList&quot;, queryList);

> In postgresql like this

 SELECT * FROM TABLE A WHERE (A.COL1, A.COL2) IN ((&#39;val1&#39;, &#39;val2&#39;), (&#39;val3&#39;, &#39;val4&#39;));

> Here is the Exception

Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: record = bytea
  建議:No operator matches the given name and argument types. You might need to add explicit type

Is this possible?

答案1

得分: 0

我会尝试按以下方式重新构造查询:

SELECT * FROM A
WHERE (A.COL1 = 'val1' and A.COL2 = 'val2')
OR (A.COL1 = 'val3' and A.COL2 = 'val4')

这将允许按以下方式构造查询:

List<String[]> queryList = new ArrayList<>();
String[] arr1 = {"val1", "val2"};
String[] arr2 = {"val3", "val4"};
queryList.add(arr1);
queryList.add(arr2);

String sql = "SELECT * FROM 表A "; //不要忘记末尾的空格

if (!queryList.isEmpty()){
    sql = sql + "WHERE "; //不要忘记末尾的空格

    for (String[] queryParam : queryList ){
        sql = sql + " (A.COL1 = '" + queryParam[0] + "' and A.COL2 = '" + queryParam[1] + "') OR "; //不要忘记末尾的空格和简单的引号
    }

    //最后移除最后一个 OR。
    Integer indexLastOR = sql.lastIndexOf("OR");
    sql = sql.substring(0, indexLastOR);
}

Query query = entityManager.createNativeQuery(sql);

这也将允许在不使用本地查询的情况下实现查询,这是维护JPA理念的建议做法。

英文:

I would try to restructure the query as follows:

SELECT * FROM TABLE A
WHERE (A.COL1 = &#39;val1&#39; and A.COL2 = &#39;val2&#39;)
OR (A.COL1 = &#39;val3&#39; and A.COL2 = &#39;val4&#39;)

This would allow the query to be constructed as follows:

List&lt;String[]&gt; queryList = new ArrayList&lt;&gt;();
String[] arr = {&quot;val1&quot;, &quot;val2&quot;};
String[] arr = {&quot;val3&quot;, &quot;val4&quot;};
queryList.add(arr);

String sql = &quot;SELECT * FROM TABLE A &quot;; //dont forget space at end

if (!queryList.isEmpty()){
    sql = sql + &quot;WHERE &quot;; //dont forget space at end


    for (String[] queryParam : queryList ){
        sql = sql + &quot; (A.COL1 = &#39;&quot;+ queryParam[0] + &quot;&#39; and A.COL2 = &#39;&quot; + queryParam[1] + &quot;&#39;) OR &quot;; //dont forget space at end and simple colons for param
    }

    //finally remove the last OR.
    Integer indexLastOR = sql.lastIndexOf(&quot;OR&quot;);
    sql = sql.substring(0, indexLastOR);
}

Query query = entityManager.createNativeQuery(sql);

This will also allow the query to be implemented without being native, which is advisable to maintain the JPA philosophy.

huangapple
  • 本文由 发表于 2020年7月24日 15:18:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63068678.html
匿名

发表评论

匿名网友

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

确定