0

我正在使用 grails 控制台来处理我的人际关系。我正在使用书中的练习Grails in Action

我有关系:

class User {
    ...

    Profile profile 

    static hasMany = [posts: Post, tags: Tag, following: User]

        ...


User.get(3).addToFollowing( User.get(2) ).save()    
User.list().each { print it.following   } 

产量

null null [com.grailsinaction.User : 2] null null

并再次运行:

User.get(1).addToFollowing( User.get(2) ).save()    
User.list().each { print it.following   } 

[com.grailsinaction.User : 2] null null null null

看起来第一个addToFollowing丢失了......我忘记了什么吗?

4

1 回答 1

1

尝试使用:

User.get(3).addToFollowing( User.get(2) ).save(flush: true)

除非使用了 flush 参数,否则该对象不会立即持久化。见相关链接

于 2013-11-11T15:12:17.830 回答