Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将空列表发送到IN子句_Java_Jpa_Spring Data Jpa_Jpa 2.0 - Fatal编程技术网

Java 如何将空列表发送到IN子句

Java 如何将空列表发送到IN子句,java,jpa,spring-data-jpa,jpa-2.0,Java,Jpa,Spring Data Jpa,Jpa 2.0,我想使用以下SQL查询: String hql = "select e from " + Terminals.class.getName() + " e WHERE e.merchantId IN :merchant_ids"; TypedQuery<Terminals> query = entityManager.createQuery(hql, Terminals.class).setParameter("merchant_ids", merchant_ids);

我想使用以下SQL查询:

String hql = "select e from " + Terminals.class.getName() + " e WHERE e.merchantId IN :merchant_ids";
        TypedQuery<Terminals> query = entityManager.createQuery(hql, Terminals.class).setParameter("merchant_ids", merchant_ids);
        List<Terminals> merchants = query.getResultList();
String hql=“从“+Terminals.class.getName()+”e中选择e,其中e.merchantId位于:商户ID”;
TypedQuery query=entityManager.createQuery(hql,Terminals.class).setParameter(“商户ID”,商户ID);
List merchants=query.getResultList();

但我得到了一个错误:
使用near')
的正确语法,因此IN子句列表into
IN(…)
不能为空。是否有解决此问题的方法?

我不熟悉hibernate,但由于它是一个SQL错误,以下方法应该可以工作:

TypedQuery<Terminals> query = entityManager
.createQuery(hql, Terminals.class)
.setParameter("merchant_ids",merchant_ids.size()==0?null:merchant_ids);
TypedQuery query=entityManager
.createQuery(hql,Terminals.class)
.setParameter(“商户ID”,商户ID.size()==0?空:商户ID);
但正如@richardbarker所提到的,最好的解决方案是在列表为空时甚至不执行查询。
当您已经知道查询不会返回任何内容时,您甚至可以保存不必要的数据库调用

很多时候,我总是用这种方法来解决问题。我找不到合适的解决办法。因为您使用的是SpringJPA,但我有一些解决方法要建议您

  • 实现EntityManger并在运行时创建SQL查询。所以你可以填充你的原因和一切。 如下所示:
    entityManager.createNativeQuery(sql.toString())

  • 执行
    if-else
    块。检查列表是否为空,如果为false,则调用实际查询(带IN块)或编写另一个不带IN块的查询


  • 我再说一遍,这可能不是一个适当的解决办法。但我认为这是适当的解决方法。

    不执行查询是允许的,甚至是非常好的:

    if (merchant_ids.isEmpty()) {
        return new ArrayList<>();
    } else {
        String hql = "select e from " + Terminals.class.getName()
                + " e WHERE e.merchantId IN :merchant_ids";
       return entityManager.createQuery(hql, Terminals.class)
            .setParameter("merchant_ids", merchant_ids)
            .getResultList();
    }
    

    我按照@Rambler的建议创建了一个返回null的方法:

        public static <T> Collection<T> nullIfEmpty(Collection<T> collection) {
            return (collection == null || collection.isEmpty()) ? null : collection;
        }
    
    公共静态集合nullIfEmpty(集合集合){
    return(collection==null | | collection.isEmpty())?null:collection;
    }
    

    这很容易添加到位,但我同意最好不要调用数据库。

    可能重复的链接我找不到解决方案。正如您所看到的,我的代码中没有
    (…)
    。当您有一个空的列表并且正在查找该列表中的某些内容时,您真的希望得到结果吗?这样的查询一定会失败。事实上,您的代码让您查找ID,如果搜索列表中没有ID,那么结果中应该没有ID。你在干什么,伙计?在我的情况下,当列表商户ID为空时,我不需要从表终端获取结果。一般的想法是,我正在寻找与指定的终端匹配的终端。仔细想想,如果您正在搜索空列表中的某个内容,您将得到一个空结果或所有结果,因此行为未定义,因此是一个错误。如何处理空列表是一个实现细节,您必须自己弄清楚并决定。嗯。。。。空参数应该使用什么?商户标识添加(0);或商户ID。添加(-1);?没有发生的值。由于ID号按惯例是非零正的,所以0也可能是零,因为java在indexOf和基于零的索引中使用了-1,我个人倾向于-1,但数据库列定义可以使用无符号数字或其他什么,
        public static <T> Collection<T> nullIfEmpty(Collection<T> collection) {
            return (collection == null || collection.isEmpty()) ? null : collection;
        }