Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - HQL pagination

Tags:

This is a problem similar to: HQL - row identifier for pagination

I'm trying to implement pagination using HQL. I have a PostgreSQL database.

int elementsPerBlock = 10; int page = 2; //offset = 2*10  String sqlQuery = "FROM Messages AS msg " +                   " LEFT JOIN FETCH msg.commands AS cmd " +                      "ORDER BY msg.identifier ASC" ;  Query query = session.createQuery( sqlQuery )                      .setFirstResult( elementsPerBlock * ( (page-1) +1 ) )                      .setMaxResults( elementsPerBlock ); 

What happens is that Hibernate fetches ALL the Messages, and returns the needed ones after they were all loaded.

Thus, Hibernate fetches 210000 entities instead of the 30 which are returned (each Messages has exactly 2 commands).

Is there a way to reduce the overhead by a factor of 7000?

edit: I've tries adding .setFetchSize( elementsPerBlock ) . It didn't help.

edit 2: the SQL query that is generated is:

select ...  from schemaName.messages messages0_  left outer join schemaName.send_commands commands1_  on messages0_.unique_key=commands1_.message_key  order by messages0_.unique_identifier ASC 

Absolutenly no LIMIT or OFFSET

like image 967
iliaden Avatar asked Jun 14 '11 18:06

iliaden


People also ask

How can we do pagination in Hibernate?

The simplest and most common way to do pagination in Hibernate is using HQL: Session session = sessionFactory. openSession(); Query query = sess. createQuery("From Foo"); query.

What is pagination in HQL?

Pagination is a simple but important feature to limit the size of your result set to a number of records that can get efficiently processed by your application and the user. You can configure it with JPA and Hibernate by calling the setFirstResult and setMaxResults on the Query or TypedQuery interface.

What is setFirstResult in Hibernate?

setFirstResult. Query setFirstResult(int firstResult) Set the first row to retrieve. If not set, rows will be retrieved beginnning from row 0. Parameters: firstResult - a row number, numbered from 0.

Does native query support pagination?

Native SQL queries The JPA query pagination is not limited to entity queries, such as JPQL or Criteria API. You can use it for native SQL queries as well.


2 Answers

Per the JPA 2.0 specification, section 3.8.6 Query Execution,

The effect of applying setMaxResults or setFirstResult to a query involving fetch joins over collections is undefined.

It varies from database to database, and in my experience, the result is Hibernate usually does the paging in memory instead of at the database query level.

What I've usually done is used a separate query to get the ids of the desired objects, and pass that into the query with the fetch join.

like image 92
Stevi Deter Avatar answered Oct 03 '22 08:10

Stevi Deter


i'm using this solution:

/**  * @param limitPerPage  * @param page  * @return  */ public List<T> searchByPage(int limitPerPage, int page, String entity) {     String sql = "SELECT t FROM " + entity + " t";     Query query = em.createQuery(sql)             .setFirstResult(calculateOffset(page, limitPerPage))             .setMaxResults(limitPerPage);     return query.getResultList(); }  /**  * @param page  * @return  */ private int calculateOffset(int page, int limit) {     return ((limit * page) - limit); } 

Welcome.

like image 26
Odravison Avatar answered Oct 03 '22 08:10

Odravison