简体   繁体   English

Hibernate(HQL)查找缺少的参数列表ID

[英]Hibernate (HQL) find missing ids of a parameter list

Let's assume i have a list of ids (as parameter, not a table) and a table with entities and their ids. 假设我有一个ID列表(作为参数,而不是一个表)和一个包含实体及其ID的表。

Parameter list: ids: 1,2,3,4 参数列表:ID:1,2,3,4

Table-Entities: ids: 1,3,4 表实体:ID:1,3,4

Now i want to find all missing values of my parameter list, that are not stored in the database, in this case: id 2. 现在,我想查找参数列表中所有未存储的值,这些值未存储在数据库中,在这种情况下:id 2。

The simple and overloaded function i have in mind is to query every single id for existence... and add it to a result if it does not exists. 我想到的简单且重载的功能是查询每个id是否存在...并将其添加到结果(如果不存在)。

public List<long> findMissing(List<long> ids) {
    List<long> missing = new ArrayList<long>();
    for (long id : ids) {
         if (!dao.exists(id))
              missing.add(id);
    }
    return missing;
}

... but i think that this is a bad idea, if the list grows (we are talking about 0 - 1000 elements). ...但是我认为这是一个坏主意,如果列表增加(我们正在谈论0-1000个元素)。

I'm wondering if there is a HQL function like this: 我想知道是否有这样的HQL函数:

SELECT t FROM (:ids) t 
     WHERE t not in 
          (SELECT e.id FROM MyEntity e 
               WHERE *maybe some conditions*);

Unfortunately, parameters are not allowed in the FROM clause. 不幸的是, FROM子句中不允许使用参数。 You will need to select all the ids and filter them in-memory. 您将需要选择所有ID,并在内存中对其进行过滤。 This shouldn't be too costly for 0-1000 elements. 对于0-1000个元素,这应该不会太昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM