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

前端 未结 1 1668
暗喜
暗喜 2021-01-26 03:13

This question is a spin-off from ANY operator with jOOQ and Are arrays optimized in jOOQ & PostgreSQL?.

I have a Field field and List<

相关标签:
1条回答
  • 2021-01-26 03:45

    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.

    0 讨论(0)
提交回复
热议问题