jOOQ字段< T> = DSL.any(DSL.val(T ...)) [英] jOOQ Field<T> = DSL.any(DSL.val(T...))

查看:185
本文介绍了jOOQ字段< T> = DSL.any(DSL.val(T ...))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是从任何有jOOQ的运算符在jOOQ& ;; PostgreSQL?

我有字段< T>字段列表< T>值我希望表达SQL identifier = any({... the values ...})。我尝试过:

I have a Field<T> field and List<T> values and I want to express SQL identifier = any({... the values ...}). I tried doing:

field.equal(DSL.any(DSL.val(values.stream().toArray())))

(注意这是通用实现的一部分,所以我没有实际的类型此时。我只有字段< T> 列表< T> 。)

(Note this is part of a generic implementation, so I don't have the actual types at this point. I only have Field<T> and List<T>.)

但这不起作用,因为API接受 T ... 而不是对象... field.equal(DSL.any(...))需要 T 。所以,我将其更改为:

But this doesn't work, since the API accepts T... instead of Object... and field.equal(DSL.any(...)) needs that T. So, I changed this to:

field.equal(DSL.any(DSL.val((T[]) values.stream().toArray())))

然而,在评论中说我不应该这样做这个。可能是一个愚蠢的问题和Java而不是jOOQ问题,但它应该怎么做?

However, in comments is said that I should not do this. Probably a dumb question and Java instead of jOOQ question, but how should it be done?

附带问题:简单地接受<$ c不是一个好主意API中的$ c>列出< T> ?这也可能会提高性能,因为我们避免手动创建数组。

Side question: isn't it a good idea to simply accept a List<T> in the API? This might also improve performance, since we are avoiding the manual array creation.

注意:同样的情况适用于 field.equal(DSL.any (values.stream()。toArray())) field.equal(DSL.any(DSL.array(values.stream()。toArray())))

Note: The same situation holds for field.equal(DSL.any(values.stream().toArray())) and field.equal(DSL.any(DSL.array(values.stream().toArray()))).

推荐答案

每当你使用jOOQ API和不安全的演员表时,你应该想知道:我使用它吗?

Every time you're using the jOOQ API with an unsafe cast, you should wonder: Am I using it right?

在你的情况下,错误在于:

In your case the mistake is here:

DSL.val((T[]) values.stream().toArray())

构造这个数组的正确方法是(假设 T 整数,这里):

The correct way to construct this array is (assuming T is Integer, here):

DSL.val(values.stream().toArray(Integer[]::new))

或者,如果是一个集合,然后更简单:

Or, if values is a Collection, then more simply:

DSL.val(values.toArray(new Integer[0]))

将正确类型的数组传递给jOOQ非常重要,因为jOOQ将使用该数组上的反射来确定它是什么数据类型,然后将其映射到例如PostgreSQL ?:: int []

It is important that you pass an array of the correct type to jOOQ as jOOQ will use reflection on that array to figure out what data type it is, and then map it to e.g. PostgreSQL ?::int[]


附带问题:不是吗在API中简单地接受 List< T> 是个好主意?这也可以提高性能,因为我们避免手动创建数组。

Side question: isn't it a good idea to simply accept a List<T> in the API? This might also improve performance, since we are avoiding the manual array creation.

问题是Java擦除了<的泛型类型信息code> T ,jOOQ需要在各种边缘情况下正确地转换绑定变量。因此,在这种情况下, T [] List< T> 更受欢迎。

The problem is that Java erases the generic type information of T, which jOOQ needs to correctly cast bind variables in various edge case situations. So, T[] is a much more preferrable type than List<T> in such cases.

这篇关于jOOQ字段&lt; T&gt; = DSL.any(DSL.val(T ...))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆