客户端排序+休眠分页? [英] Client Side sorting + Hibernate Paging?

查看:21
本文介绍了客户端排序+休眠分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GWT 作为 UI,使用 Hibernate/Spring 作为业务层.以下 GWT 小部件用于显示记录.(http://collectionofdemos.appspot.com/demo/com.google.gwt.gen2.demo.scrolltable.PagingScrollTableDemo/PagingScrollTableDemo.html).我假设排序是在客户端完成的.

I use GWT for UI and Hibernate/Spring for buisness-layer.Following GWT widget is used to display the records.(http://collectionofdemos.appspot.com/demo/com.google.gwt.gen2.demo.scrolltable.PagingScrollTableDemo/PagingScrollTableDemo.html).I assume the sorting is done in client side.

我不检索整个结果集,因为它很大.我用

I do not retrieve the entire result set since its huge. I use

principals = getHibernateTemplate().findByCriteria(criteria,
                    fromIndex, numOfRecords);

检索数据.在Hibernate层中没有排序标准.

to retrive data.Theres no criteria for sorting in Hibernate layer.

这种方法没有给出正确的行为,因为它只对客户端中的当前数据集进行排序.

This approach does not give the correct behaviour since it only Sorts the current dataset in the client.

这个问题的最佳解决方案是什么?

What is the best solution for this problem?

注意:我可以使用 UI 框架获取主排序列和其他排序列.我可以在休眠层中使用主排序列对结果进行排序吗?

NOTE : I can get the primary-Sort-column and other sort Columns using the UI framework. May be I can sort the result using primary-sort-column in the hibernate layer?

推荐答案

您需要在服务器上进行排序.

You need to sort on the server.

然后你可以:

  • 将完整的结果集发送给客户端并在客户端处理分页.问题是结果集可能很大,无法从数据库中检索并发送到客户端.

  • send the complete resultset to the client and handle pagination on the client side. The problem is that the resultset may be big to retrieve from db and sent to the client.

在服务器端处理分页.客户端和服务器一次只从数据库请求一页.那么问题是,每次向数据库询问特定页面时,您都会一次又一次地订购相同的数据以提取第 1 页、第 2 页等.这可能是大型数据库的问题.

handle the pagination on the server side. The client and the server request only one page at a time from the db. The problem then is that you will order the same data again and again to extract page 1, page 2, etc. each time you ask the db for a specific page. This can be a problem with large database.

在两者之间进行权衡(对于大型数据库):

have a trade-off between both (for large database):

  • 设置一个限制,比如 300 件
  • 服务器根据 by 的顺序向数据库询问前 301 项
  • 服务器将结果集(最多 301 个项目)保存在缓存中
  • 客户端逐页请求服务器
  • 服务器使用缓存处理分页
  • 如果有 301 个项目,客户端会显示命中列表包含 300 多个项目.已被截断".

注1:通常情况下,客户端不会在乎他是否无法进入最后一页.您可以改进解决方案以首先计算总行数(那时不需要排序),以便您可以向用户显示更好的消息,例如结果包含2023个元素,只能查看前300个".

Note 1: Usually, the client doesn't care if he can't go to the last page. You can improve the solution to count for the total number of rows first (no need of order by then) so that you can display message that is better to the user, e.g. "Result contained 2023 elements, only first 300 can be viewed".

注意2:如果你在数据库中逐页请求数据而不使用任何顺序标准,大多数数据库(至少是Oracle)不保证any顺序.因此,如果您向数据库发出两个请求,则第 1 页和第 2 页中可能有相同的项目.如果多个项目具有用于订购的相同值(例如相同日期),则会发生同样的问题.db 不保证具有相同值的元素之间的任何排序.如果是这种情况,我会建议使用 PK 作为最后一个排序标准(例如 ORDER BY date, PK),以便以一致的方式完成分页.

Note 2: if you request the data page by page in the database without using any order criterion, most db (at least Oracle) don't guarantee any ordering. So you may have the same item in page 1 and 2 if you make two requests to the database. The same problem happens if multiple items have the same value that is use to order by (e.g. same date). The db doesn't guarantee any ordering between element with the same value. If this is the case, I would then suggest to use the PK as the last order criterion to order by (e.g. ORDER BY date, PK) so that the paging is done in a consistent way.

注意 3:我说的是客户端和服务器,但您可以根据自己的特定情况调整这个想法.

Note 3: I speak about client and server, but you can adapt the idea to your particular situation.

这篇关于客户端排序+休眠分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆