Grails/GORM where 子句如何设置 fetchMode 属性

发布于 2024-12-28 04:19:13 字数 493 浏览 2 评论 0原文

在 Grails 2 中,我们都知道 where 子句创建 DetachedCriteria。此外,在创建普通 Criteria 查询时,还可以设置关系的 fetchMode 属性。但是,如果我要创建 where 子句查询,如何设置基础 Criteria 的 fetchMode 属性?

不起作用:

MyDomainClass.where {
  foo == 'bar'
  fetchMode "reference", FetchMode.EAGER
}

起作用,但使用旧的 Criteria 样式,而不是 where 子句:

MyDomainClass.withCriteria {
  eq "foo", "bar"
  fetchMode "reference", FetchMode.EAGER
}

请不要告诉我在域类映射中将获取模式设置为 eager。我知道这一点,并且需要在查询级别控制急切的获取。谢谢!

In Grails 2, we all know that a where clause creates a DetachedCriteria. Also, one can set the fetchMode property of a relationship when creating a normal Criteria query. But, if I'm creating a where-clause query, how do I set the fetchMode property of the underlying Criteria?

Does not work:

MyDomainClass.where {
  foo == 'bar'
  fetchMode "reference", FetchMode.EAGER
}

works, but uses old Criteria style, not where clause:

MyDomainClass.withCriteria {
  eq "foo", "bar"
  fetchMode "reference", FetchMode.EAGER
}

Please don't tell me to set the fetch mode to eager in the domain class mapping. I know about that, and need to control the eager fetching at the query level. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2025-01-04 04:19:13

尚不支持此功能,因此请在 http://jira.grails.org/browse 创建功能请求/GRAILS

通过访问执行实际查询的 Hibernate Criteria 实例,有一个有点黑客的解决方法:

MyDomainClass.where {
   foo == 'bar'
}.withPopulatedQuery(null, null) { query ->
   [email protected]('reference', FetchMode.EAGER)
   query.list()
}

This isn't supported yet, so please create a feature request at http://jira.grails.org/browse/GRAILS

There is a somewhat hackish workaround though by accessing the Hibernate Criteria instance that does the actual querying:

MyDomainClass.where {
   foo == 'bar'
}.withPopulatedQuery(null, null) { query ->
   [email protected]('reference', FetchMode.EAGER)
   query.list()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文