简体   繁体   English

JOOQ“IN”查询抛出空指针异常

[英]JOOQ "IN" Query throws null pointer exception

When we try to fetch data with Null values当我们尝试使用 Null 值获取数据时

field(TABLE_NAME.COLUMN_NAME.in(null))

with IN clause getting null pointer exception.使用 IN 子句获取空指针异常。

Maybe because of this.也许是因为这个。

  @Override
    public final Condition in(Collection<?> values) {
        Field<?>[] fields = new Field[values.size()];

        Iterator<?> it = values.iterator();
        for (int i = 0; it.hasNext(); i++)
            fields[i] = Tools.field(it.next(), this);

        return in(fields);
    }

In the database, we can provide null in IN clause.在数据库中,我们可以在 IN 子句中提供 null。

There is an existing "won't fix" issue in jooq https://github.com/jOOQ/jOOQ/issues/3867 jooq https://github.com/jOOQ/jOOQ/issues/3867 中存在一个“无法修复”的问题

There are some alternatives:有一些替代方案:

  • check null before IN(Cant do in my case its a really big select statement)在 IN 之前检查 null(不能在我的情况下做它一个非常大的选择语句)

So if I want to make this possible is there any other workaround.因此,如果我想让这成为可能,是否还有其他解决方法。

PS: On a similar note "eq" works perfectly fine: PS:在类似的说明中,“eq”工作得很好:

@Override
    public final Condition equal(Field<T> field) {
        return compare(EQUALS, nullSafe(field, getDataType()));
    }

Edit: 'field(TABLE_NAME.COLUMN_NAME.in(null))' here null is a collection.编辑: 'field(TABLE_NAME.COLUMN_NAME.in(null))' 这里的 null 是一个集合。

Your example code doesn't compile:您的示例代码无法编译:

TABLE_NAME.COLUMN_NAME.in(null)

There are 5 overloads of this in() method in jOOQ 3.14, and as such, you cannot pass the null literal to the in() method.这个in()方法在 jOOQ 3.14 中有 5 个重载,因此,您不能将null文字传递给in()方法。 Your real client code may be using a local variable like this:您的真实客户端代码可能正在使用这样的局部变量:

Collection<?> collection = null;
TABLE_NAME.COLUMN_NAME.in(collection)

There might be a case for when this should behave the same as passing an empty collection, such as Collections.emptyList() , but this isn't what you seem to want.在某些情况下,这应该与传递空集合(例如Collections.emptyList()行为相同,但这似乎不是您想要的。 You probably want to pass actual null values inside of that collection, which you can do:您可能希望该集合中传递实际的null值,您可以这样做:

TABLE_NAME.COLUMN_NAME.in(1, null, 2)

But why would you do it?但你为什么要这样做? SQL implements three valued logic , meaning that NULL values have no effect in IN predicates, while they have an unintuitive, hardly desired effect in NOT IN predicates (the entire predicate becomes NULL ) SQL 实现了三值逻辑,这意味着NULL值在IN谓词中没有影响,而它们在NOT IN谓词中具有不直观的、几乎不需要的效果(整个谓词变为NULL

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

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