GORM in Grails and StaleObjectStateException

痴心易碎 提交于 2019-12-05 05:45:33

I'm not exactly sure why you're hitting issues, but there is a merge method on domain objects. It lets you reattach the current object to the current persistent context.

I don't know enough about what your Foo, or what customizations you've done to the User object, or the version of grails/java you're using to be able to reproduce this.

I'm thinking it has something to do with the refreshing you're doing on the user object which is causing the database version to get updated (and thus out of sync), but I can't be sure.

Also, I believe that the validate then discard behavior is changing and less necessary in grails 1.1 based on the comments on this post

One thing I notice is that you're not saving user, even though you've just added some foo to it. In fact, saving user should obviate the need to save foo.

You don't have to validate user: it's not having its properties changed by an untrusted source, and the database-level constraints are checked anyway whenever you save.

Lastly, things like user.refresh() are better moved outside your actions and into an interceptor or a filter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!