我正在使用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
丢失了...我忘记了什么吗?
最佳答案
尝试使用:
User.get(3).addToFollowing( User.get(2) ).save(flush: true)
除非使用flush参数,否则不会立即保留该对象。 请参阅相关的link。
关于grails - addTo *不保留更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19908762/