What your query effectively does is this:

SELECT COUNT(*) FROM (
    SELECT COUNT(*)
    FROM TODOS
    WHERE ...
)


So, you've successfully created a "value 1 generator", as that's indeed the
only value that can ever come out of that query. :-)

But I can easily see how this fetchCount() method is confusing. It was
intended as a convenience for writing

jooq.fetchCount(select().from(TODOS).where(...));


... in case of which the semantics is clear. But since there is also a
possibility to write

List<Integer> counts =
jooq.select(count())
    .from(TODOS)
    .where(...)
    .fetch(count());


... the confusion is perfect.

Thanks for reporting! I guess we'll have to remove this Select.fetchCount()
query again. We'll deprecate it for future versions of jOOQ:
https://github.com/jOOQ/jOOQ/issues/3356

It's quite contrary to "ordinary jOOQ intuition". I'm sorry for the
inconvenience this has caused.

Best,
Lukas

2014-06-25 18:58 GMT+02:00 <[email protected]>:

> I ran into a really weird problem when I tried to implement a count query
> with jOOQ:
>
> It doesn't matter how many rows the *todos* table has. My count query
> always returns 1. The cleaned up version of my repository looks as follows:
>
>
> import org.jooq.Condition;
> import org.jooq.DSLContext;
>
> import org.springframework.stereotype.Repository;
> import org.springframework.transaction.annotation.Transactional;
>
> import static net.petrikainulainen.spring.jooq.todo.db.tables.Todos.TODOS;
>
> @Repository
> public class JOOQTodoRepository implements TodoRepository {
>
>     private final DSLContext jooq;
>
>     //Removed constructor for the sake of clarity
>
>     @Transactional(readOnly = true)
>     @Override
>     public long findCountBySearchTerm(String searchTerm) {
>         String likeExpression = "%" + searchTerm + "%";
>
>            return jooq.selectCount()
>                .from(TODOS)
>                .where(
>                        TODOS.DESCRIPTION.likeIgnoreCase(likeExpression)
>                         .or(TODOS.TITLE.likeIgnoreCase(likeExpression)
>                 )
>                .fetchCount();
>     }
> }
>
> I am using jOOQ 3.3.2 and H2 version 1.3.174. All help is appreciated :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "jOOQ User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to