HQL连接查询,渴望获取大量的关系 [英] HQL joined query to eager fetch a large number of relationships

查看:105
本文介绍了HQL连接查询,渴望获取大量的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目最近发现Hibernate可以采用多级关系,并渴望在单个连接HQL中获取它们,以产生我们需要的填充对象。我们喜欢这个功能,认为它会超越懒惰的抓取环境。

My project has recently discovered that Hibernate can take multiple levels of relationship and eager fetch them in a single join HQL to produce the filled object we need. We love this feature, figuring it would outperform a lazy fetch circumstance.

问题是,我们碰到一个单亲有大约十几个直接关系,几个子关系,其中有几个在几个实例中有几十行。结果是一个相当大的交叉产品,导致hql旋转它的轮子实际上永远。在我们放弃并杀死它之前,我们已经将日志记录数量增加到了11,并且查看了超过100000次迭代。

Problem is, we hit a situation where a single parent has about a dozen direct relationships, an a few subrelationships off of that, and a few of them have several dozen rows in a few instances. The result is a pretty large cross-product that results in the hql spinning it's wheels virtually forever. We turned logging up to 11 and saw more than 100000 iterations before we gave up and killed it.

很清楚,虽然这种技术对于某些情况是很好的,像生活中的一切。但是在hibernate中,这是最好的替代方法是什么?我们不想延迟加载这些,因为我们会陷入N + 1的情况,会更糟。

So clearly, while this technique is great for some situations, it has limits like everything in life. But what is the best performing alternative in hibernate for this? We don't want to lazy-load these, because we'll get into an N+1 situation that will be even worse.

我理想地喜欢Hibernate预取所有的行和细节,但一次只做一个关系,然后将正确的细节对象水化到正确的父,但我不知道它是否有这样的事情。

I'd ideally like to have Hibernate pre-fetch all the rows and details, but do it one relationship at a time, and then hydrate the right detail object to the right parent, but I have no idea if it does such a thing.

建议?

UPDATE:

所以我们得到了这个查询生成的SQL,结果我误解了这个问题。交叉产品不是那么巨大。我们在数据库中直接运行相同的查询,并在一秒钟内返回500行。

So we got the SQL this query generated, it turns out that I misdiagnosed the problem. The cross product is NOT that huge. We ran the same query in our database directly and got 500 rows returned in just over a second.

然而,我们在hibernate日志中非常清楚地看到它做了100K次迭代。是否可能Hibernate可能陷入一个循环在你的关系或什么?

Yet we saw very clearly in the hibernate logging it making 100K iterations. Is it possible Hibernate can get caught in a loop in your relationships or something?

或者也许这应该被问为一个新问题?

Or maybe this should be asked as a new question?

推荐答案

我们的团队使用特殊策略与协会合作。集合是惰性的,单个关系也是惰性的,除了引用简单的结构(例如国家参考)。我们使用流利 - 休眠加载我们在具体情况下需要的内容。这只是因为流利的hibernate支持嵌套的投影。您可以参考此单元测试看看如何复杂的对象网可以部分加载。单元测试的代码段

Our team uses the special strategy to work with associations. Collections are lazy, single relations are lazy too, except references with simply structure (for an example a countries reference). And we use fluent-hibernate to load what we need in a concrete situation. It is simply because of fluent-hibernate supports nested projections. You can refer this unit test to see how complex object net can be partially loaded. A code snippet from the unit test

 List<Root> roots = H.<Root> request(Root.class).proj(Root.ROOT_NAME)
            .innerJoin("stationarFrom.stationar", "stationar")
            .proj("stationar.name", "stationarFrom.stationar.name")
            .eq(Root.ROOT_NAME, rootName).transform(Root.class).list();

另请参阅

如何使用Hibernate转换平面结果集

这篇关于HQL连接查询,渴望获取大量的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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