1

我有一个与使用连接表Campaign具有多对多关系的实体。ContactRecipient

class Campaign {
    ...
    static hasMany = [recipients: Recipient]
    ...
}

class Recipient {
    ...
    Contact contact
    static belongsTo = [campaign: Campaign]
    ...
}

class Contact {
    ...
}

营销活动可以有数十万个联系人,但由于延迟获取是默认设置,因此可以快速从数据库中检索营销活动。但是,当我调用campaign.refresh()GORM 尝试加载所有收件人时,Grails 内存不足。

List<Campaign> campaigns = Campaign.findAllWhere([status: CampaignStatus.STARTED])
campaigns.each { campaign ->
    if (campaign.refresh().isStarted()) {
        campaign.send();
    }
}

我打开了 logSql,看看正在执行什么。此代码已被清理以提高可读性。

-- Campaign.findAllWhere
select * from campaign where (status=?)

-- campaign.refresh()
select * from campaign c left outer join recipient r on c.campaign_id=r.campaign_id where c.campaign_id=?

为什么刷新加入收件人但查找查询不是?

4

1 回答 1

1

For reference:

It is generally problematic to have huge collections mapped into a domain with hasMany/belongsTo since most operations are cascaded to the collection. This is why recipients are refreshed in your case but similar problems may arise in other business code around this domain.

This is not really GORM specific (though the cascading is set by GORM automatically) since these kinds of embedded collections would be mapped similarly in pure Hibernate/Java code.

In this case, it is better to separate Recipients from Campaign and not have the embedded collection at all in Campaign.

You may also try to define cascading manually for the recipients collection (and specifically leave out refresh)

于 2014-09-30T12:36:13.983 回答