如何在hibernate中限制Object属性中的集合? [英] How to limit a collection in an Object's property in hibernate?

查看:149
本文介绍了如何在hibernate中限制Object属性中的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体:商品和出价,每个商品都有很多出价,因此出价是商品的集合属性。

I have two entities: item, and bid, each item have many bids for it, so bid is a collection property of item.

在显示的页面中一个项目,我只是想显示关于这个项目的前10个出价记录。

in the page that show an item, I just want to show the first 10 records of bids about this "item".

所以,我做这个查询:

from item
left join fetch item.bids
where item.id=3
...

但这会获取有关商品的所有出价,
所以,如何限制商品的出价?

but this will fetch all bids about an item, so, how to limit the bids about an item ?

推荐答案

如果您还有从Bid到Item的关联,那么效果会更好。

This works so much better if you also have an association from Bid to Item.

然后你可以选择项目并应用限制限制:

Then you can select the items and apply the limit restriction:

session
    .createQuery(
    "select b
    from bid b
    join fetch b.item i
    where 
        i.id=3")
    .setMaxResult(10)
    .list();

您之前的查询,选择项目和获取投标的查询将始终选择所有项目及其所有出价和在内存中应用最大结果限制。这是因为Hibernate必须总是提取所有孩子,它不能给你部分收集结果。

Your previous query, the one selecting Item and fetching Bids will always select all items with all their bids and apply the max result limit in-memory. This is because Hibernate must always fetch all children, it can't give you partial collection results.

选择孩子并加入父母是一个更好的选择。该限制适用于所选儿童,不受任何限制。

Selecting the child and joining the parent is a better alternative. The restriction applies to the selected children with no restriction whatsoever.

这篇关于如何在hibernate中限制Object属性中的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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