Grails mongodb插件,处理关系周期

时间:2013-01-03 13:41:49

标签: mongodb grails associations gorm

鉴于社交网络中的个人资料:

Class Profile {
   String name
   List<Profile> friends

   static hasMany = [friends:Profile]
}

想象一下,配置文件A是配置文件B的朋友,反之亦然:

a.addToFriends(b)
b.addToFriends(a)

加载

等个人资料时
def p = Profile.get(id)

循环导致堆栈溢出,因为所有关联都被加载到任何深度。看起来mongodb plugin不支持延迟加载,这会阻止恶意加载周期。我的灵魂是使用低级gmongo API,在friends集合中存储ObjectID并在ProfileService.getProfileById()方法中手动加载关联。这工作正常,但我更喜欢尽可能使用GORM。现在我根本不能使用GORM用于具有可能周期的类。有没有办法教mongodb插件延迟加载?我已经尝试将fetchMode设置为lazy但它只是不起作用....任何想法?

1 个答案:

答案 0 :(得分:0)

据我所知,Grails至少有2个MongoDB Gorm插件。如果您使用the one based on morphia,则可以通过@Reference(lazy=true)定义延迟引用。