翻译结果:JPA分页每个请求是否具有相同的顺序

huangapple go评论58阅读模式
英文:

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(&quot;select * from Table&quot;);
int pageNumber = 6;
int pageSize = 10;
query.setFirstResult((pageNumber-1) * pageSize); 
query.setMaxResults(pageSize);
List &lt;Foo&gt; 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.

huangapple
  • 本文由 发表于 2023年6月13日 16:12:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定