2

Giving the last example given in this SO thread.

I get this error:

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.example.dtos.UserElement (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.example.dtos.UserElement is in unnamed module of loader 'app')

Am I supposed to use a special mapper that jooq will be happy with?

Any help is welcome

Edit:

Jooq version: 3.14.3 Postgres: 11

ctx.select(
    USERS.ID.`as`("id"),
    USERS.USERNAME.`as`("name"),
    field(select(jsonArrayAgg(
        jsonObject(
            jsonEntry("id", USER_ELEMENT.ID),
            jsonEntry("name", USER_ELEMENT.ELEMENTS)
        )
    )
).from(USER_ELEMENT).where(USER_ELEMENT.ID
    .`in`(
        select(USER_ELEMENT_ACTION.USER_ELEMENT_ID)
            .from(USER_ELEMENT_ACTION)
            .where(USER_ELEMENT_ACTION.USER_ID.eq(USERS.ID))
        )
    )).`as`("elements")
).from(USERS)
.apply { if (condition != null) where(condition) }
.fetchInto(UserRoles::class.java)

dto

data class UserRoles(val id: Int, val name: String, val elements: List<UserElement>?)
data class UserElement(val id: Int, val name: String)

I've also tried to use SimpleFlatMapper

ctx.configuration().set(JooqMapperFactory.newInstance().newRecordMapperProvider())

but get

MapperBuildingException: Could not find Getter for JooqFieldKey{field="elements", index=2} returning type

I'm using spring boot and here are my jackson dependencies:

implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.10.5")
implementation("com.fasterxml.jackson.core:jackson-databind:2.10.0")

Stacktrace:

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.example.dtos.UserElement (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.example.common.dtos.UserElement is in unnamed module of loader 'app')
at com.example.dao.UserDao.findUserAuthorities(UserDao.kt:103) ~[main/:na]
at com.example.service.auth.AuthService.getUserDetailByUsername(AuthService.kt:34) ~[main/:na]
at com.example.security.AuthUserDetailService.loadUserByUsername(WebSecurityConfigurer.kt:58) ~[main/:na]
at com.example.security.jwt.JwtRequestFilter.passAuthToTheSecurityContext(JwtRequestFilter.kt:48) ~[main/:na]
at com.example.security.jwt.JwtRequestFilter.doFilterInternal(JwtRequestFilter.kt:35) ~[main/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:92) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:77) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178) ~[spring-security-web-5.3.5.RELEASE.jar:5.3.5.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93) ~[spring-boot-actuator-2.3.5.RELEASE.jar:2.3.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.10.RELEASE.jar:5.2.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.39.jar:9.0.39]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
aiqency
  • 1,015
  • 1
  • 8
  • 22
  • What JOOQ version are you using, which database, and which JSON library? In general, please show us your current code to make sure we're all talking about the exact same thing. – Petr Janeček Nov 20 '20 at 14:57
  • Thanks for the edit. Does this really produce the mentioned exception? I don't see 1) where you would have a `LinkedHashMap` in this example, 2) there's no type `Role`. What is the *exact* exception you're getting? – Lukas Eder Nov 20 '20 at 15:25
  • @LukasEder Thks for the quick feedback. In fact I get the exception as soon as I want to loop through the `elements`. The debugger shows that the list of `elements` has not been mapped to a List of `UserElement` but to a LinkedHashMap. While using `SimpleMapper` the `elements` field is null. – aiqency Nov 20 '20 at 15:30
  • I see, interesting. The approach you've chosen works if you have gson or Jackson on your classpath. Do you? How is "SimpleMapper" (SimpleFlatMapper?) involved here? Who is producing this `LinkedHashMap`? I don't think jOOQ does that... Can you post the full stack trace here? – Lukas Eder Nov 20 '20 at 15:39
  • I've updated the error message. – aiqency Nov 20 '20 at 15:54
  • 1
    Notice that jOOQ 3.15 now has a better way to map to nested Java collections, if you don't need the intermediate JSON representation: https://blog.jooq.org/2021/07/06/jooq-3-15s-new-multiset-operator-will-change-how-you-think-about-sql – Lukas Eder Jul 07 '21 at 11:24

1 Answers1

1

jOOQ 3.14 added support for mapping JSON to arbitrary classes via gson or Jackson out of the box. This is documented here: https://www.jooq.org/doc/latest/manual/sql-execution/fetching/converter-provider

Notice this is a per-column data type conversion feature, which is invoked by jOOQ's DefaultConverterProvider, not a RecordMapper feature. After more discussions, this appears to be a bug in the RecordMapper, which doesn't provide Jackson with as much generic type information as would be possible in your case:

The bug has been fixed in jOOQ 3.15.0 and 3.14.10

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • I guess I will be able to fix it thanks to your converter-provider link. Thank you very much. – aiqency Nov 20 '20 at 16:07
  • @aiqency: Just to be sure: You shouldn't have to do anything other than adding Jackson (and possibly removing SimpleFlatMapper) to your classpath for this to work. If it still doesn't, maybe there's a bug? Feel free to report one here: https://github.com/jOOQ/jOOQ/issues/new/choose, ideally with an MCVE that helps reproduce your exact setup (template here: https://github.com/jOOQ/jOOQ-mcve) – Lukas Eder Nov 20 '20 at 16:15
  • Did you try it on your side? I get another error: Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.app.dao.UserRoles` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"id" : 1, "name" : "admin", "elements" : [{"id" : 1, "name" : "element1"}, {"id" : 5, "name" : "element5"}, ...]. I guess it is not a jooq issue anyway. – aiqency Nov 24 '20 at 15:40