使用NHibernate时限制结果数 [英] Limiting number of results when using NHibernate

查看:68
本文介绍了使用NHibernate时限制结果数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道人们通常如何限制查询返回的实体数量.我知道SetMaxResults,但是一旦涉及到联接,它似乎就会崩溃-每个2个子级的3个实体将产生6行.如果我使用SetMaxResults(3)仅获得3个实体,那么我将仅获得3个结果行,这可能会给我一个不完整的对象图.

I was wondering how people usually go about limiting the number of entities returned from a query. I know about SetMaxResults but this seems to break down as soon as joins are involved - 3 entities with each 2 children will produce 6 rows as a result. If I use SetMaxResults(3) to get only 3 entities I will only get 3 resulting rows which will probably give me an incomplete object graph.

我知道这是由于DistinctRootEntityResultTransformer引起的,但我想知道其他人为避免获得1000行只是为了获得建立3个实体所需的6行而做了什么.

I am aware this is due to the DistinctRootEntityResultTransformer, but I'm wondering what other people do to avoid getting 1000 rows just to get the 6 rows necessary to build my 3 entities.

推荐答案

我的答案可能不是100%准确,因为我是Java Hibernate类型的人,但是我猜测NHibernate具有与它完全相同的设计Java表弟.

My answer may not be 100% accurate as I'm a Java Hibernate type of guy, but I'm guessing that NHibernate has the exact same design as it's java cousin.

Hibernate保证查询结果中的项目数将完全等于查询返回的行数.一切正常,因为查询通常会为每个实体返回一行,当查询联接到另一个表并产生笛卡尔积时就会出现问题.

Hibernate promises that the number of items in the result of a query will be exactly equal to the number of rows returned from the query. This is all fine as normally the query returns one row for each entity, the problem comes in when the query joins to another table and produces the cartesian product.

选项对:

  1. 从查询中删除所有加入"条件/条款.如果只是为了提高性能而包括在内,这可能很容易,请注意,可以将它们替换为不会创建联接的'fetch'子句
  2. 将sql限制添加到在最终sql的where子句中创建子查询的条件中

请注意,这对于冬眠或冬眠并不是特别重要的,对联接结果集进行分页是完全不可能的(没有蛮力和无知)

Note this isn't particalar to hibernate or nhibernate, pagign a joined result set is just plain impossiable (with out brute force and ignorance)

希望这会有所帮助

这篇关于使用NHibernate时限制结果数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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