Java 我们可以通过使用命名查询来限制记录吗?

Java 我们可以通过使用命名查询来限制记录吗?,java,Java,我的数据库中大约有20000条记录,但我一次只想获取500条记录。我使用命名查询获取数据。那么,如何限制命名查询 我试着这样做 @NamedQuery(name = "qryChildEventByOrganization", query = "select childEvent from ChildEvent childEvent limit 500" + "left outer join fetch childEvent.child as ceChild " + "left outer j

我的数据库中大约有20000条记录,但我一次只想获取500条记录。我使用命名查询获取数据。那么,如何限制命名查询

我试着这样做

@NamedQuery(name = "qryChildEventByOrganization", query = "select childEvent from ChildEvent childEvent  limit 500"
+ "left outer join fetch childEvent.child as ceChild "
+ "left outer join fetch ceChild.address as address "
+ "left outer join fetch ceChild.user as cuser "
+ "left outer join fetch childEvent.user as ceuser "
+ "left outer join fetch cuser.address as caddress "
+ "left outer join fetch ceuser.address as ceaddress "
+ "left outer join fetch ceChild.immunization as cimmunization "
+ "left outer join fetch ceChild.insurance as cinsurance "
+ "left outer join fetch ceChild.healthHistory as chealthHistory "
+ "left outer join fetch ceChild.emergencyContact as cemergencyContact "
+ "left outer join fetch ceChild.doctor as cdoctor "
+ "left outer join fetch ceChild.dietActivityRestriction as cdietActivityRestriction "
+ "left outer join fetch ceChild.allergy as cAllergy "
+ "left outer join fetch ceChild.medication as cMedication "
+ "left outer join fetch ceChild.provider as cProvider "
+ "left join fetch childEvent.event as ceEvent "
+ "left join fetch ceEvent.organization as eOrganization "
+ "left join fetch eOrganization.address as oAddress "
+ "left outer join fetch ceChild.childEvent as cChildEvent "
+ "left join fetch cChildEvent.event as ceEventChildCe "
+ "left join fetch ceEventChildCe.organization as eCeOrganization "
+ "left join fetch eCeOrganization.address as oCeAddress "
+ "left outer join fetch cChildEvent.child as cCEChild "
+ "left outer join fetch cCEChild.address as address "
+ "left outer join fetch cCEChild.user as cuser "
+ "left outer join fetch cuser.address as ceaddress "
+ "left outer join fetch cCEChild.immunization as cimmunization "
+ "left outer join fetch cCEChild.insurance as cinsurance "
+ "left outer join fetch cCEChild.healthHistory as chealthHistory "
+ "left outer join fetch cCEChild.emergencyContact as cemergencyContact "
+ "left outer join fetch cCEChild.doctor as cdoctor "
+ "left outer join fetch cCEChild.dietActivityRestriction as cdietActivityRestriction "
+ "left outer join fetch cCEChild.allergy as cAllergy "
+ "left outer join fetch cCEChild.medication as cMedication "
+ "where childEvent.event.organization.organizationId = :organizationId AND (childEvent.status is null OR childEvent.status LIKE 'Active')"),

您可以使用hibernate条件查询语言实现此任务,如下所示:-

Crietria c=session.createCriteria(XYZ.class);  
c.setFirstResult(1);  
c.setMaxResult(500);  

我认为你需要重构你的关系。如此多的连接既令人难以置信,又是性能杀手。