jooq throws NPE when fetchOne is used

Viewed 2755

I have a simple query on a table which queries with the primary key.

            dslContext.select(XXX.NAME)
                .from(XXX)
                .where(
                    XXX.ID.eq(id)
                        .and(XXX.LEVEL.eq(2))
                        .and(XXX.NAME.isNotNull())
                )
                .fetchOne().into(String.class);

In my case for a particular id, the query results in a empty set. But jooq seems to throw a NPE. When I further investigated, fetchOne() calls CursorImpl.fetchOne(). This checks the size of the result and if it is not 1, it returns null. I have a chained into(String.class) which gets called on this null value and hence resulting in NPE.

I don't want to call fetch() and iterate over the results/get the first element of the list.

Is there an alternative way of writing the query such that it will throw a org.jooq.exception.NoDataFoundException if there is no data?

1 Answers
Related