简体   繁体   English

Grails GORM JOIN查询

[英]Grails GORM JOIN in query

I have an entity that is join entity between Notification and User : 我有一个实体,它是NotificationUser之间的Notification实体:

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']
    }
}

I want to create query that receive all notifications assigned to the specified user, but I want notification field to be populated to avoid N+1 selects while iterate over result (1 query for retreive list and one select per each notification field access with default lazy loading) as mentioned http://grails.org/doc/2.3.7/guide/single.html#criteria in Querying with Eager Fetching section. 我想创建一个查询,以接收分配给指定用户的所有通知,但是我想填充notification字段,以避免在对结果进行迭代时重复进行N + 1个选择(1个查询检索列表,每个通知字段选择1个查询,默认为惰性加载),如http://grails.org/doc/2.3.7/guide/single.html#criteria在“ 使用快速获取进行查询”部分中所述。

I don't want to change mapping and set notification to lazy: false , but specify it in query ( http://grails.org/doc/2.3.7/guide/single.html#fetchingDSL ). 我不想更改mapping并将notification设置为lazy: false ,而是在查询中指定它( http://grails.org/doc/2.3.7/guide/single.html#fetchingDSL )。

My query is (as docs says): 我的查询是(如文档所说):

def resut = NotificationRecipient.createCriteria().list(options?.params ?: [:]) {
    eq("recipientUser.id", userId)
    join 'notification'
}

but still N+1 queries are generated. 但仍然会生成N + 1个查询。

I have also tried with setting Fetch mode: 我也尝试设置获取模式:

NotificationRecipient.createCriteria().list(options?.params ?: [:]) {
    eq("recipientUser.id", userId)
    fetchMode "notification", FetchMode.JOIN
}

without success. 没有成功。

Can you explain me this behaviour of Grails? 您能解释一下Grails的这种行为吗?

Unfortunalelly it is a Hibernate or Grails bug: 不幸的是,这是一个Hibernate或Grails错误:

http://jira.grails.org/browse/GRAILS-9285 http://jira.grails.org/browse/GRAILS-9285

The solution is remove compound primary key id generator: 'assigned', composite: ['notification', 'recipientUser'] . 解决的方法是删除复合主键id generator: 'assigned', composite: ['notification', 'recipientUser']

For compoun primary key joins doesn't work. 对于复合主键联接不起作用。 Also order by is not working properly. 另外订购依据不能正常工作。

Have you tried using: 您是否尝试过使用:

NotificationRecipient.findAllByRecepientUser(user, [fetch:[notification:'eager']])

A similar request works great (without the N+1) on my system. 类似的请求在我的系统上效果很好(没有N + 1)。

I would also check if there are lazy fetched parameters on Notification (the Notification is loaded eagerly, but some properties in notifications are not simple types but Domain objects that need to be lazy loaded). 我还要检查Notification上是否有延迟获取的参数(Notification急切地加载,但是通知中的某些属性不是简单类型而是需要延迟加载的Domain对象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM