Spring 在JPA中实现分页的正确方法是什么?
我查看了这个问题的一些细节,但在实现分页时仍然有问题。我认为这可能与我的实体经理的设置方式有关 我使用Spring来配置应用程序;以下是相关的spring配置(必须删除一些细节):Spring 在JPA中实现分页的正确方法是什么?,spring,hibernate,jpa,paging,entitymanager,Spring,Hibernate,Jpa,Paging,Entitymanager,我查看了这个问题的一些细节,但在实现分页时仍然有问题。我认为这可能与我的实体经理的设置方式有关 我使用Spring来配置应用程序;以下是相关的spring配置(必须删除一些细节): org.hibernate.dialen.oracle10galent 真的 org.hibernate.cache.EhCacheProvider 3. 100 1000 真的 假的 org.hibernate.ejb.HibernatePersistence 下面是调用的方法,它试图完成分页: public
org.hibernate.dialen.oracle10galent
真的
org.hibernate.cache.EhCacheProvider
3.
100
1000
真的
假的
org.hibernate.ejb.HibernatePersistence
下面是调用的方法,它试图完成分页:
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;
}
public void execute(){
createBaseDir();//在文件系统上创建一个目录
文件domainDir=createDomainDir();//创建上面创建的目录的子文件夹
Long numRows=countRows();//下面提供了实现
//这就是我试图寻呼的地方
//em是Spring使用@PersistenceContext注入的EntityManager
CriteriaBuilder cb=em.getCriteriaBuilder();
//IEntity是所有托管实体从中继承的自定义接口。
//clazz是我要查询的托管实体的FQCN
CriteriaQuery>c=cq.from(clazz);
cq.选择(c);
cq.orderBy(cb.asc(c.get(“id”));
TypedQuery>entities=entityQuery.getResultList();
marshallToFile(entities,domainDir);//使用jaxb将域对象整理成文件
}
}
私有长countRows(){
CriteriaBuilder=em.getCriteriaBuilder();
CriteriaQuery countQuery=builder.createQuery(Long.class);
//clazz是我要查询的托管实体的FQCN
选择(builder.count(countQuery.from(clazz));
Long count=em.createQuery(countQuery).getSingleResult();
返回计数;
}
问题在于,在for循环的每次迭代中,对entityQuery.getResultList()的调用代码>向结果列表中再添加100行,而不是返回仅包含100行的列表。我使用了一个调试器,并且setFirstResult
和setMaxResults
工作正常(第一次迭代是0,100;然后是100200;然后是200300;等等),但是返回的列表的大小每次都会增加
有人知道我的问题是什么吗?Doh!发现我在逻辑上的缺陷…:/
我使用了一个调试器,并且setFirstResult
和setMaxResults
工作正常(第一次迭代是0,100;然后是100200;然后是200300;等等)
这是不对的。。。应该是0100,;然后是100100;然后是200100;等等
无论如何,我必须改变这一行:
intmaxresults=(行+页面大小>numRows)?numRows.intValue():行+页面大小代码>
为此:
intmaxresults=(行+页面大小>numRows)?numRows.intValue():页面大小代码>
现在它开始工作了。我想我会把这个作为一个例子留给其他人如何让分页工作
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;
}