最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jooq json converter not working when query: class org.jooq.JSONB cannot be cast to class java.util.List - Stack Overflow

programmeradmin14浏览0评论

I can save data into postgres using jsonConverter with following setting

 <forcedType>
     <userType>java.util.List&lt;com.sify.MyType&gt;</userType>
     <jsonConverter>true</jsonConverter>
     <includeExpression>table\.field</includeExpression>
</forcedType>

However, if I select data from postgres, jsonb columns aren't converted to the List<MyType>. It reports error: class org.jooq.JSONB cannot be cast to class java.util.List, when calling TableRecord.getField() method.


Edit: I tried using a custom binding like this: , the behavior is the same


Edit: I checked the generated code, TableRecord.getField() is just

    public List<Field> getField() {
        return (List<Field>) get(12);
    }

Maybe it should be get(12, (Class<List<Field>>) (Class<?>) List.class) instead?

I can save data into postgres using jsonConverter with following setting

 <forcedType>
     <userType>java.util.List&lt;com.sify.MyType&gt;</userType>
     <jsonConverter>true</jsonConverter>
     <includeExpression>table\.field</includeExpression>
</forcedType>

However, if I select data from postgres, jsonb columns aren't converted to the List<MyType>. It reports error: class org.jooq.JSONB cannot be cast to class java.util.List, when calling TableRecord.getField() method.


Edit: I tried using a custom binding like this: https://github.com/jOOQ/jOOQ/issues/9679, the behavior is the same


Edit: I checked the generated code, TableRecord.getField() is just

    public List<Field> getField() {
        return (List<Field>) get(12);
    }

Maybe it should be get(12, (Class<List<Field>>) (Class<?>) List.class) instead?

Share Improve this question edited Feb 5 at 7:50 sify asked Feb 5 at 6:54 sifysify 74111 silver badges21 bronze badges 3
  • This works for me as expected. Please provide a complete reproducer. – Lukas Eder Commented Feb 5 at 8:55
  • @LukasEder github.com/sify21/testjooq, JOOQ 3.20.0-SNAPSHOT is built locally with this commit: 0596e787acbc444f00399edeaca28e05e7deed82 – sify Commented Feb 5 at 9:42
  • I see, I thought this was about a compilation error, not a runtime exception. – Lukas Eder Commented Feb 5 at 13:40
Add a comment  | 

1 Answer 1

Reset to default 1

jOOQ cannot pass along a Class<List<MyType>> reference to Jackson, because the generics are erased by the Java compiler, so Jackson will simply receive Class<List>. This is different if you map your JSONB document to a com.sify.MyType[] array type, which is still available at runtime.

I don't think there will be an easy solution using the out-of-the-box JSONBtoJacksonConverter implementation in jOOQ 3.20, but do have a look at its implementation. You can probably easily implement a Converter<JSONB, List<MyType>> by working with the Jackson API directly.

For a future version of jOOQ, there might be an out-of-the-box heuristic that tries to pass List type information to Jackson CollectionType, or similar:

  • https://github.com/jOOQ/jOOQ/issues/17953

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论