Search code examples
javajpatypesjooq

Does the List<Object> returned by org.jooq.Query.getBindValues() contain type-safe objects - for each query parameter?


New to JOOQ. Came across this post: https://thoughts-on-java.org/hibernate-jooq-a-match-made-in-heaven/

`

SelectConditionStep<Record3<String, String, String>> jooqQuery = 
        ctx.select(AUTHOR.FIRSTNAME, AUTHOR.LASTNAME, BOOK.TITLE)
            .from(AUTHOR)
                .leftJoin(BOOK_AUTHOR).on(AUTHOR.ID.eq(BOOK_AUTHOR.AUTHORS_ID))
                .leftJoin(BOOK).on(BOOK_AUTHOR.BOOKS_ID.eq(BOOK.ID))
            .where(AUTHOR.FIRSTNAME.like("Thor%"));
Query q = em.createNativeQuery(jooqQuery.getSQL());
setBindParameterValues(q, jooqQuery);


private static void setBindParameterValues(javax.persistence.Query jpaQuery, org.jooq.Query jooqQuery) {
    List<Object> values = jooqQuery.getBindValues();
    for (int i = 0; i < values.size(); i++) {
        jpaQuery.setParameter(i + 1, values.get(i));
    }
}

`

The method jpaQuery.setParameter() itself is not-type-safe as it accepts 'Object'.

Question: Does jooqQuery.getBindValues() return List<Object> which are "type-safe for each-param"?


Solution

  • That method is the glue code the author of the blog post wrote to convert a jOOQ Query to a JPA Query. There is no type safety in this glue code. The type safety you are looking for was provided in other examples of the post, where the author constructed type safe queries using the jOOQ API, such as this:

    // This stuff is type safe
    var jooqQuery = 
            ctx.select(AUTHOR.FIRSTNAME, AUTHOR.LASTNAME, BOOK.TITLE)
                .from(AUTHOR)
                    .leftJoin(BOOK_AUTHOR).on(AUTHOR.ID.eq(BOOK_AUTHOR.AUTHORS_ID))
                    .leftJoin(BOOK).on(BOOK_AUTHOR.BOOKS_ID.eq(BOOK.ID))
                .where(AUTHOR.FIRSTNAME.like("Thor%"));
    
    // This stuff isn't, and there is no need
    Query q = em.createNativeQuery(jooqQuery.getSQL());
    setBindParameterValues(q, jooqQuery);