【发布时间】:2014-04-25 21:47:38
【问题描述】:
我有一个实体,它是 Notification 和 User 之间的连接实体:
class NotificationRecipient implements Serializable {
Notification notification
User recipientUser
static mapping = {
table 'notification_recipient'
version false
notification cascade: 'all'
recipientUser cascade: 'all'
id generator: 'assigned', composite: ['notification', 'recipientUser']
}
}
我想创建一个查询来接收分配给指定用户的所有通知,但我希望填充 notification 字段以避免在迭代结果时进行 N+1 次选择(1 个查询用于检索列表,每个通知一个选择使用默认延迟加载的字段访问),如 Querying with Eager Fetching 部分中提到的 http://grails.org/doc/2.3.7/guide/single.html#criteria。
我不想更改 mapping 并将 notification 设置为 lazy: false,但在查询中指定它 (http://grails.org/doc/2.3.7/guide/single.html#fetchingDSL)。
我的查询是(如文档所述):
def resut = NotificationRecipient.createCriteria().list(options?.params ?: [:]) {
eq("recipientUser.id", userId)
join 'notification'
}
但仍会生成 N+1 个查询。
我也尝试过设置获取模式:
NotificationRecipient.createCriteria().list(options?.params ?: [:]) {
eq("recipientUser.id", userId)
fetchMode "notification", FetchMode.JOIN
}
没有成功。
你能解释一下 Grails 的这种行为吗?
【问题讨论】:
标签: hibernate grails grails-orm