springhibernatejpapaginationentitymanager

What is the correct way to implement paging in JPA?


I looked at this question for some of the details but I'm still having a problem implementing paging. I think it might have something to do with the way my entity manager is set up.

I'm using Spring to configure the application; here is the relevant spring configuration (had to remove some details):

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">


    <import resource="classpath:core-infrastructure-context.xml" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan" value="domain" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">100</prop>
                <prop key="hibernate.jbc.batch_size">1000</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">false</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="databaseMarshaller" >
        <property name="pageSize" value="100"/>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="validationQuery" value=""/>
        <property name="testWhileIdle" value="false"/>
    </bean>

</beans>

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

    <persistence-unit name="persistenceUnit">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    </persistence-unit>

</persistence>

And here is the method that is called which is trying to accomplish the paging:

public void execute(){

    createBaseDir(); //creates a directory on the filesystem

    File domainDir = createDomainDir(); //creates a subfolder of the directory created above

    Long numRows = countRows(); //implementation provided below


    //Here is where I'm trying to page
    //em is an EntityManager injected by Spring using @PersistenceContext
    CriteriaBuilder cb = em.getCriteriaBuilder();

    //IEntity is a custom interface that all managed entities inherit from.
    //clazz is the FQCN of the managed entity I am querying for
    CriteriaQuery<IEntity<?>> cq = cb.createQuery(clazz);
    Root<IEntity<?>> c = cq.from(clazz);
    cq.select(c);
    cq.orderBy(cb.asc(c.get("id")));
    TypedQuery<IEntity<?>> entityQuery = em.createQuery(cq);

    //pageSize = 100, injected from Spring
    for(int row = 0; (row + pageSize) < numRows || numRows-row > 0; row+=pageSize){

        entityQuery = em.createQuery(cq);
        entityQuery.setFirstResult(row);
        int maxResults = (row+pageSize > numRows)?numRows.intValue():row+pageSize;
        entityQuery.setMaxResults(maxResults);



        //This call grows by pageSize every iteration instead of returning only #pageSize records
        List<IEntity<?>> entities = entityQuery.getResultList(); 

        marshallToFile(entities, domainDir);//Uses jaxb to marshall domain objects to file

    }
}

private Long countRows(){
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    //clazz is the FQCN of the managed entity I am querying for
    countQuery.select(builder.count(countQuery.from(clazz)));
    Long count = em.createQuery(countQuery).getSingleResult();
    return count;
}

The problem is that on each iteration of the for loop, the call to entityQuery.getResultList(); adds 100 more rows to the result list instead of returning a list containing only 100 rows. I stepped through with a debugger and the setFirstResult and setMaxResults is working correctly (first iteration they are 0, 100; then 100,200; then 200,300; etc...) but the size of the list being returned grows each time.

Anyone see what my problem is?


Solution

  • Doh! Spotted my flaw in logic... :/

    I stepped through with a debugger and the setFirstResult and setMaxResults is working correctly (first iteration they are 0, 100; then 100,200; then 200,300; etc...).

    That is not correct... it should be 0,100; then 100,100; then 200,100; etc...

    Anyways, had to change this line: int maxResults = (row+pageSize > numRows)?numRows.intValue():row+pageSize; to this: int maxResults = (row+pageSize > numRows)?numRows.intValue():pageSize;

    And now it works. Guess I'll leave this up as an example to others on how to get paging working.