Grails GORM条件查询渴望获取 [英] Grails GORM Criteria Query Eager Fetching

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

问题描述

我在Grails服务类中编写了一个条件查询,希望在该类中执行急切的连接,并在将结果显示为JSON响应或GSP时避免延迟加载子对象.查询按预期执行(在DataSource.groovy中设置hibernate.show_sql = true可以看到查询),但是当我在GSP中抓取关联时,我可以看到Hibernate正在执行后续查询,就像延迟加载一样协会.我不相信这种急切的加载实际上是有效的.我不想在我的域类中为这些关联设置lazy:false.

I have written a criteria query in a Grails service class where I expect an eager join to be performed, and to avoid lazy loading of child objects when displaying my results either as a JSON response or in my GSP. The query executed as expected (setting my hibernate.show_sql=true in my DataSource.groovy I can see the query), but when I crawl the association in my GSP, I can see that Hibernate is executing subsequent queries as if it were lazily loading the associations. I'm not convinced that the eager loading is actually working. I do not want to set lazy:false within my domain class for these associations.

这是条件查询:

def market = Market.withCriteria(uniqueResult:true){
    idEq(marketId)
    fetchMode 'resourceAssignments', FetchMode.JOIN
    fetchMode 'resourceAssignments.userRole', FetchMode.JOIN
    fetchMode 'resourceAssignments.userRole.role', FetchMode.JOIN
    fetchMode 'resourceAssignments.userRole.user', FetchMode.JOIN
    resourceAssignments{
        userRole{
            role{
                'in'('name', roleNames)
            }
        }
    }           
}

上面的查询返回没有任何问题.但是,当我尝试运行以下代码时,在我的GSP中,我可以看到Hibernate正在发出第二个查询,就像它在懒惰地获取resourceAssignments一样:

The above query returns without any issues. When I attempt to run the following code, though, in my GSP, I can see that Hibernate is issuing a second query as if it were lazily fetching the resourceAssignments:

<g:each in="${market.resourceAssignments}" var="ra">
</g:each>

我什至尝试通过创建一个空的WebRequestInterceptor并在resources.groovy中将openSessionInViewInterceptor设置为使用它来使用No-Op拦截器覆盖OpenSessionInViewInterceptor.完成该操作后,我得到一个org.hibernate.LazyInitializationException似乎可以验证我的想法-即使我指定要热切获取这些关联,Hibernate或GORM仍在尝试执行第二个查询. /p>

I even tried overriding the OpenSessionInViewInterceptor with a No-Op interceptor, by creating an empty WebRequestInterceptor and setting the openSessionInViewInterceptor in the resources.groovy to use it. Once I did that, I get a org.hibernate.LazyInitializationException which seems to verify what I was thinking - that Hibernate or GORM is still trying to execute a second query even when I've specified that I want to eagerly fetch these associations.

推荐答案

它似乎是带有条件查询的Grails错误.这是一个可以使用的HQL查询:

It appears to be a Grails bug with criteria queries. Here's an HQL query that works though:

def market = Market.executeQuery(
    'select m from Market m ' +
    'inner join fetch m.resourceAssignments as ra ' +
    'inner join fetch ra.userRole as ur ' +
    'inner join fetch ur.role as role ' +
    'inner join fetch ur.user as user ' +
    'where m.id=:marketId and role.name in (:roleNames)',
    [marketId: marketId, roleNames: roleNames], [max: 1])[0]

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

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