如何使用对象列表作为一组条件查询表格?

huangapple 未分类评论46阅读模式
英文:

HQL How to query Table with List of Objects as a set of conditions?

问题

我有一个名为form的类

    类 Form {
        字符串 id;
        字符串 version;
        字符串 minVersion;  
    }
现在我手头上有一个`List<Form>`。
我必须查询一个满足条件的表使得idversiontype作为一组满足即我想从我的列表中找到表中完全相同的所有{idversionminVersion}例如

我有一个这样的表格

<pre>
+----+---------+------------+---------+
| id | version | minVersion | passage |
+----+---------+------------+---------+
|  1 |       2 |          2 | Hi      |
|  1 |       3 |          1 | Hello   |
|  2 |       2 |          2 | Hi      |
|  3 |       3 |          3 | Hi      |
+----+---------+------------+---------+
</pre>

我想从中选择2行作为List<Form>{id: 1version: 3minVersion: 1}{id: 2version: 2minVersion: 2} 

我写了一个像这样的查询

    SELECT idversiontypepassage FROM Content WHERE (idversiontype) IN (:id:version:minVersion);
但是如何转换为HSQL并为其设置参数
英文:

I have a class called form

Class Form {
    String id;
    String version;
    String minVersion;  
}

Now I have a List<Form> with me.
I have to query a table which satisfies the condition such that the id, version, type all as a set satisfies ie. I want to find all the {id, version, minVersion} which is the exact same in the table from my list. For example

I have a table like this

<pre>
+----+---------+------------+---------+
| id | version | minVersion | passage |
+----+---------+------------+---------+
| 1 | 2 | 2 | Hi |
| 1 | 3 | 1 | Hello |
| 2 | 2 | 2 | Hi |
| 3 | 3 | 3 | Hi |
+----+---------+------------+---------+
</pre>

I want to select 2 rows from it given as a List<Form>: {id: 1, version: 3, minVersion: 1} and {id: 2, version: 2, minVersion: 2}

I wrote a query like this,

SELECT id, version, type, passage FROM Content WHERE (id, version, type) IN (:id, :version, :minVersion);

But how do to convert into HSQL and setParameter for it?

答案1

得分: 0

建议:如果您的表格大小较小,请获取与特定id相关的所有元素,并在Java层面进行过滤。

请在您的查询中使用IN语法和setParameterList API。

例如:

String hql = "SELECT id, version, type, passage FROM Content WHERE id IN (:ids)";

在构建查询时请使用

Query query = session.createQuery(hql);
query.setParameterList("ids", idList);

参考此链接

英文:

Suggestion if your table size is small get all the elements specific to ids and filter in java layer

Please use the IN syntax in your query and setParameterList API.

For Example:

String hql = " SELECT id, version, type, passage FROM Content WHERE id in (:ids) "

And while forming query use

Query query = session.createQuery(hql);
query.setParameterList(&quot;ids&quot;, idList);

Refer this

答案2

得分: 0

尝试一下:

List<Form> findByIdInAndVersionInAndMinVersionIn(List<String> ids, List<String> versions, List<String> minVersions);
英文:

Try this:

List&lt;Form&gt; findByIdInAndVersionInAndMinVersionIn(List&lt;String&gt; ids, List&lt;String&gt; versions, List&lt;String&gt; minVersions);

huangapple
  • 本文由 发表于 2020年4月6日 20:29:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/61059899.html
匿名

发表评论

匿名网友

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

确定