我看着this問題的一些細節,但我仍然有一個問題實施分頁。我認爲這可能與我的實體經理成立的方式有關。在JPA中實現分頁的正確方法是什麼?
我使用Spring來配置應用程序;這裏是有關Spring配置(不得不刪除一些細節):
<?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>
這裏是調用的方法正試圖實現分頁:
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;
}
的問題是,每個迭代for循環時,對entityQuery.getResultList();
的調用會將100多行添加到結果列表中,而不是返回僅包含100行的列表。我通過一個調試器,setFirstResult
和setMaxResults
工作正常(第一次迭代它們是0,100;然後是100,200;然後200,300;等等),但是每次返回列表的大小都會增加。
有人看到我的問題是什麼?