jOOQ Field<T> = DSL.any(DSL.val(T…))

社会主义新天地 提交于 2019-12-02 09:22:33

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())

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]))

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[]

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!