英文:
Does paging JPA have same order each request
问题
我使用JPA从数据库获取页面结果
查询查询 = entityManager.createNativeQuery("select * from Table");
int pageNumber = 6;
int pageSize = 10;
查询.setFirstResult((pageNumber-1) * pageSize);
查询.setMaxResults(pageSize);
List<Foo> fooList = 查询.getResultList();
我有一个问题,第x页(例如:6页)的结果每次请求都是相同的吗?
我测试了每次请求都有相同的结果,但我不确定。
如果我使用“ORDER BY”,我认为结果是相同的,但如果在本地查询中没有使用“ORDER BY”,我不确定。
英文:
I use JPA to get page result from DB
Query query = entityManager.createNativeQuery("select * from Table");
int pageNumber = 6;
int pageSize = 10;
query.setFirstResult((pageNumber-1) * pageSize);
query.setMaxResults(pageSize);
List <Foo> fooList = query.getResultList();
I have question, does result in page x (ex:6) always same items each request?
I tested it have same result a page each request but I'm not sure.
If I use "ORDER BY" I think is same but I'm not sure if not have "ORDER BY" in native query
答案1
得分: 1
Without ORDER BY
clause the order of the select statement is undefined. It can be ordered by ID
or other column due to an index defined. But you have no guarantee it will remain the same with each call.
The only guarantee of having a defined order is by adding ORDER BY
clause.
This is not related to JPA
. It is just pure SQL
specification.
英文:
Without ORDER BY
clause the order of the select statement is undefined. It can be ordered by ID
or other column due to an index defined. But you have no guarantee it will remain the same with each call.
The only guarantee of having a defined order is by adding ORDER BY
clause.
This is not related to JPA
. It is just pure SQL
specification.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论