我有一个域对象用户
static hasMany = [following:User]
当我创建一个新用户时,我可以在我的多选中选择我想要关注的用户并且它可以正常工作但是如果我然后更新我的用户并更改'跟随'列表,那么使用
java.lang.IllegalStateException:无法转换类型的值 [org.codehaus.groovy.grails.web.binding.ListOrderedSet]要求 输入属性'following'的[java.util.Set]:没有匹配的编辑器或 发现转换策略
在UserController.update(UserController.groovy:76)at java.util.concurrent.ThreadPoolExecutor中的$ Worker.runTask(ThreadPoolExecutor.java:886) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:908) 在java.lang.Thread.run(Thread.java:68
gsp代码
<div class="clearfix ${hasErrors(bean: userInstance, field: 'following', 'error')} ">
<label for="following">
<g:message code="user.following.label" default="Following" />
</label>
<div class="input">
<g:select name="following" from="${com.social.User.list()}" multiple="multiple"
optionKey="id" size="5" value="${userInstance?.following*.id}" class="many-to-many" />
</div>
</div>
以下是更新的控制器代码:
def update() {
def userInstance = User.get(params.id)
if (!userInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])
redirect(action: "list")
return
}
if (params.version) {
def version = params.version.toLong()
if (userInstance.version > version) {
userInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'user.label', default: 'User')] as Object[],
"Another user has updated this User while you were editing")
render(view: "edit", model: [userInstance: userInstance])
return
}
}
userInstance.properties = params
// It will work if I blacklist the following param as below
//bindData(userInstance, params, ['following'])
if (!userInstance.save(flush: true)) {
render(view: "edit", model: [userInstance: userInstance])
return
}
flash.message = message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])
redirect(action: "show", id: userInstance.id)
}
我目前有一个
的解决方法// Auto saving of following seems to fail so blacklist following param
// then set the list manually...
bindData(userInstance, params, ['following'])
userInstance.following = User.getAll(params.get("following") as List<BigInteger>)
在用户控制器内的更新但似乎可能有更好的方法???
答案 0 :(得分:0)
我遇到了这个问题,它最终是由json-rest-api插件导致的,该插件无法正确处理从数据库中获取的hibernate代理。