Grails:渴望获取一对一的manies [英] Grails: Eager fetching of one-to-manies

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

问题描述

在阅读 GORM Gotchas 时,我对于急切地提取一对一男士有点困惑。鉴于上面提到的链接:


请考虑以下查询:



Author.list(max:2,fetch:[books:'join']) $ b

很可能,这将只返回一个Author实例。那
可能不是你期望或想要的行为。那么发生了什么?



Hibernate正在使用左外连接为每个作者获取书籍
。这意味着您会得到重复的作者实例:作者与每本书相关联的每本书都有一个
如果您没有max
选项,则不会看到这些重复内容,因为GORM删除
他们。但麻烦的是,最大选项应用于结果
删除重复项之前。所以在上面的例子中,Hibernate
只返回两个结果,这两个结果可能有相同的
作者。 GORM然后删除重复项,并最终得到一个
Author实例。


上面引号的粗体部分是我的困惑开始的地方。如果它是一个左外连接,我们不应该得到完全由每个作者完成的书(只有一个作者的书),而不是由多个作者创作的书(集合的交集我们没有进入左外连接)?或者,这暗示着可能由多个作者通过 [books:'join'] 查询修饰符处理书籍的内容。



我很乐意澄清。 解决方案

混淆来自您不理解正在使用 LEFT JOIN 执行的SQL获取书籍列表。这个评论试图指出的是,通过使用 LEFT JOIN MAX 会带来意想不到的结果。考虑以下几点:$ b​​
$ b

  author 
id,名字
1,Bob
2,Joe

book
id,name
1,第一本书
2,第二本
3,第三本
4,第四本

author_books
author_id,book_id
1,1
1,2
1,3
2,4

查询在获取Author和它的Book关联时使用 LEFT JOIN 从数据库返回如下所示:

  author.id,author.name,book.id,book.name 
1,Bob,1,第一本书
1,Bob,2,第二本书

这将导致单个作者被返回,但书籍集合将只包括三本相关书籍中的两本。这是由于 MAX 限制被应用于SQL语句本身并限制了返回的最大行数。



之所以这样指出,是因为您可能期望不管关联图书的数量多少,您的列表中最多可以获得两位作者,但是由于获取模式不是因为正在执行的实际SQL。



希望有帮助。


While reading about the GORM Gotchas I am a bit confused about eager fetching of one-to-manies. Whereas it is stated at the aforementioned link:

consider this query:

Author.list(max: 2, fetch: [ books: 'join' ])

In all likelihood, this will return only one Author instance. That’s probably not the behaviour you expect or want. So what’s happening?

Under the hood Hibernate is using a left outer join to fetch the books for each author. That means you get duplicate Author instances: one for each book the author is associated with. If you don’t have the max option there, you won’t see those duplicates because GORM removes them. But the trouble is the max option is applied to the result before the duplicates are removed. So in the example above, Hibernate only returns two results, both of which are likely to have the same author. GORM then removes the duplicate and you end up with a single Author instance.

The bolded portion of the above quote is where my confusion begins.If it is a left outer join shouldn't we end up getting books that are solely by every author(books only by one author) and not books that are by more than one author (the intersection of the sets which we don't get in a left outer join)? Or is this implying something about the treatment of books that maybe by more than one author by the [ books: 'join'] query modifier.

I'd be much obliged for any clarification.

解决方案

The confusion comes from your not understanding the SQL that is being executed using a LEFT JOIN to fetch the list of books. What the comment is trying to point out is that by using a LEFT JOIN the MAX gives you unexpected results. Consider the following

author
id, name
1, Bob
2, Joe

book
id, name
1, First book
2, Second book
3, Third book
4, Fourth book

author_books
author_id, book_id
1, 1
1, 2
1, 3
2, 4

The query uses a LEFT JOIN when fetching the Author and it's Book association so the data being returned from the database looks like this:

author.id, author.name, book.id, book.name
1, Bob, 1, First book
1, Bob, 2, Second book

This will result in a single author being returned but the collection of books will only include two of the three associated books. This is due to the MAX restriction being applied to the SQL statement itself and limiting the maximum number of rows returned.

The reason why this is pointed out is because you might expect that you'd get a maximum of two Authors in your list regardless of the number of associated books, but because of the fetch mode that's not the case because of the actual SQL that is being executed.

Hope that helps.

这篇关于Grails:渴望获取一对一的manies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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