感谢您的关注,抱歉 mi English :S
我正在使用 JPA 2.0 和 Hibernate 4.X 来执行一些 sql 本机查询。代码很简单:
private void doIt() throws Exception {
EntityManager em = getEntityManager();
Query q = em.createNativeQuery("A very simple native query, which return no entities, but collections of arrays");
q.setFirstResult(0);
q.setMaxResults(5);
Collection<Object> results = q.getResultList();
System.out.println("1"); //Means page 1
for (Object elem : results) {
String line = "";
Object[] row = (Object[]) elem;
for (Object object : row) {
if(object==null){
object="null";
}
line += object +"("+object.getClass()+")"+ ",";
}
System.out.println(row.length + " " + line);
}
em = getEntityManager();
q = em.createNativeQuery("The same simple native query, which return no entities, but collections of arrays");
q.setFirstResult(5);
q.setMaxResults(5);
results = q.getResultList();
System.out.println("2"); //Means page 2
for (Object elem : results) {
String line = "";
Object[] row = (Object[]) elem;
for (Object object : row) {
if(object==null){
object="null";
}
line += object +"("+object.getClass()+")"+ ",";
}
System.out.println(row.length + " " + line);
}
em = getEntityManager();
q = em.createNativeQuery("The same simple native query, which return no entities, but collections of arrays");
q.setFirstResult(10);
q.setMaxResults(5);
results = q.getResultList();
System.out.println("3"); //Means page 3
for (Object elem : results) {
String line = "";
Object[] row = (Object[]) elem;
for (Object object : row) {
if(object==null){
object="null";
}
line += object +"("+object.getClass()+")"+ ",";
}
System.out.println(row.length + " " + line);
}
}
我的结果是这样的:
1
data1,data2,...,data-n -->I need this output
data1,data2,...,data-n
data1,data2,...,data-n
data1,data2,...,data-n
data1,data2,...,data-n
2
data1,data2,...,data-n,6 -->OMG! lol
data1,data2,...,data-n,7
data1,data2,...,data-n,8
data1,data2,...,data-n,9
data1,data2,...,data-n,10
3
data1,data2,...,data-n,11
data1,data2,...,data-n,12
data1,data2,...,data-n,13
data1,data2,...,data-n,14
data1,data2,...,data-n,15
简而言之,第一页的输出每行有n个项目(这是我想要的输出),但是第二页和第三页有n+1个项目,并且附加的项目似乎是已经带来的行数。
有人发生过同样的事情吗?我在 Hibernate 的文档中进行了搜索,但我无法解决这个“有趣的:@”问题。
此代码是使用 Toplink 执行的,它没有问题。
非常感谢你!!:)