Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-263

Add support for manually specifying count query when the results of JPA criteria query are paginated

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 1.1.2
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Hibernate 4.1.6.Final

      Description

      If you want to paginate the query results of a JPA criteria query, you have to call the findAll(Specification s, Pageable p) method that is declared by the JpaSpecificationExecutor interface. The implementation of this method uses the provided Specification for two purposes: It gets the count of the matching entities and the actual entities.

      This approach works fine in most of the cases. However, if you have to join a lazily fetched collection of the entity, a following exception is thrown (when Hibernate is used as JPA provider):

      org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list

      In this case, the following query is created:

      select count(*) from foo.bar.Person as generatedAlias0 
      left join fetch generatedAlias0.pets as generatedAlias1 where
      generatedAlias0.state=:param1
      

      As the exception explained, the problem is that the Person is not present in the select list of the count query.

      In this case it would useful if you could manually specify the specification that is used to create the count query. This way you create two specifications: the first one would be used to create the count query (no join needed) and the second one would fetch the actual data (joins can be used).

        Attachments

          Activity

            People

            Assignee:
            schauder Jens Schauder
            Reporter:
            pkainulainen Petri Kainulainen
            Last updater:
            Jens Schauder
            Votes:
            7 Vote for this issue
            Watchers:
            11 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: