简体   繁体   中英

Spring boot + Hibernate + SqlAzure pagination issue

I have a java spring boot application that use hibernate as ORM. The database is an Azure SQL Server.

I've enabled the setShowSql on vendor adapter configuration.

When I want to find objects, I used the TypedQuery's methods setFirstResult and setMaxResults and than invoke the getResultList method.

The query printed in the console doesn't contains the OFFSET and ROW FETCH clauses and it seems that Hibernate first retrieve all result and than apply the pagination on the resulted list.

This obviously causes performance issues.

Where am I doing wrong?

Below I report the sample code I used.

query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
...
query.getResultList()

The only time Hibernate will explicitly include the OFFSET and FETCH clauses under a SQL Server dialect will be when the following conditions are met:

  1. You must use org.hibernate.dialect.SQLServer2012Dialect or any future 2012+ version.
  2. Your query must include an ORDER BY clause.
  3. Your query is not executing a TOP clause query.

The SQLServer2012Dialect uses a customized LimitHandler implementation called SQLServer2012LimitHandler that you can see here that explicitly handles this use case or otherwise falls back to the old behavior.

If both of the requirements above are being met but the logic is still fallig back to the old behavior for some reason, then it's a bug. In that case, you probably should update HHH-12152 with a test case so we can fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM