分页问题,​​同时加入Hibernate Criteria [英] Pagination Issue while join in Hibernate Criteria

查看:74
本文介绍了分页问题,​​同时加入Hibernate Criteria的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题是:

我想在多表加入标准后分页。 >当我加入表格时生成重复记录。 适用于记录集的分页(带有重复)



我使用 criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 删除重复的记录。



例如:
通常,我在应用删除重复项后获得了10条记录。
当我设置我的开始索引为1,结束索引为5时,我应该得到5条记录,但它返回2或3(大约)。这取决于加入。



请帮我解决问题。

解决方案

这里的问题在于应用两种不同的技术。


  • 首先,数据库服务器上正确应用了分页。它将返回预定的行数(例如5)。

  • 第二部分是应用程序部分,Hibernate确实从这5条记录中选择了DISTINCT值。



因此,如果实际上有2行加1另一行,转换将导致3个对象。



正确的(也许是最好的)方法,如何避免这种情况,是不使用集合的提取。如果我们需要收集显示,我们应该加载它懒惰地(例如使用批量来减少选择的数量)

如果我们需要收集作为过滤器,我们应该将它转换为子查询,并且再次在根实体上执行分页,并且IN(子查询)子句已到位



试想一下,父表:

  ParentId,代码
1,'P1'
2,'P2'
3,'P3'

子表: p>

  ChildId,Code,ParentId 
1,'C1',1
2,'C2',1
3,'C3',2
4,'C4',2
5,'C5',3

如果我们要求父母并加入小孩集合,我们将在数据库服务器上收到5行


  1. ,它将只被转换为Ap上的3个不同的父对象折level级别

  2. I want to have the pagination after the multiple table join in criteria.

    The issue is:

    The duplicate records generated when I join the tables. The pagination applied to the record set(With duplication).

    I use this criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); to remove the duplicated records.

    For example: Normally I got 10 records after applying the removing duplication. When I set my start index as 1 and end index as 5 , I am supposed to get 5 records but it returns 2 or 3(approx). It depends on the joining.

    Please help me out.

    解决方案

    The problem here is in the two different techniques beeing applied.

    • Firstly there is a pagination correctly applied on the DB Server. It will return the intended number of rows (e.g. 5).
    • The second part is the Application part, where the Hibernate does select DISTINCT values from these 5 records.

    So if there are in fact 2 rows doubled plus 1 other, the transformation will result in 3 objects.

    The correct (and maybe the best) way, how to avoid that, is to not use fetching of the collections. If we need collection to be displayed, we should load it lazily (e.g. using batch-size to reduce number of selects)

    If we need collection to be used as a filter, we should convert it into subquery, and again do the pagination on the root entity, with the IN (Subquery) clause in place

    Imagine this, Parent table:

    ParentId, Code
    1       , 'P1' 
    2       , 'P2'
    3       , 'P3'
    

    The child table:

    ChildId , Code , ParentId
    1       , 'C1' , 1
    2       , 'C2' , 1
    3       , 'C3' , 2
    4       , 'C4' , 2
    5       , 'C5' , 3
    

    If we will ask for a Parent and also join the Child collection, we will

    1. recieve 5 rows on the DB server,
    2. which will be converted into only 3 Distinct Parent objects on the Application level

    这篇关于分页问题,​​同时加入Hibernate Criteria的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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