gpt4 book ai didi

hibernate - 麻烦将参数绑定(bind)到对象并成功保存()

转载 作者:行者123 更新时间:2023-12-02 15:00:33 24 4
gpt4 key购买 nike

我有一个基本的 Q/A 应用程序,但在更改答案的顺序时遇到了问题。答案通过 Ordinal 显示。具有 unique constraint 的属性.

答案域

class Answer {

DateTime dateCreated
DateTime lastUpdated

String body
Integer ordinal
String reason

static belongsTo = [question: Question]

static constraints = {
body blank: false
ordinal unique: 'question'
}

String toString() {
"Answer: $body"
}
}

问题域
class Question {

DateTime dateCreated
DateTime lastUpdated

String body
Answer correctAnswer
Integer ordinal

static belongsTo = [lesson: Lesson]
static hasMany = [answers: Answer]

static constraints = {
body blank: false
correctAnswer nullable: true,
validator: { Answer val, Question obj ->
val ? val.question == obj : true // TODO: Give this a proper error message
}
ordinal unique: 'lesson'
}

static mapping = {
lesson lazy: true
answers sort: 'ordinal'
}
}

我通过表格更新序数

form image
<g:each status="i" in="${questionInstance.answers}" var="answer">
<input type="number" name="answers[${i}].ordinal" value="${answer?.ordinal}" />: ${answer}
</g:each>

更新 Action

参数绑定(bind)到 questionInstance.answers对象并更新 ordinal字段每个答案
def update(Long id, Long version) {

def questionInstance = Question.get(id)

questionInstance.properties = params

if (!questionInstance.save(flush: true)) {
render(view: "edit", model: [questionInstance: questionInstance])
return
}

redirect(action: "index", id: questionInstance.id)
}

这就是我的 questionInstance.answers对象在我绑定(bind) params 之前的样子
before

params 之后被分配。请注意 answers已从 hibernate.collection.PersistentSet 更改至 grails.web.binding.ListOrderedSet
after

堆栈跟踪
No signature of method: edu.example.work_department.website.Question$__clinit__closure1_closure3.doCall() is applicable for argument types: (edu.example.work_department.website.Answer, edu.example.work_department.website.Question) values: [Answer: Answer 2, edu.example.work_department.website.Question : 1]
Possible solutions: doCall(edu.example.work_department.website.Answer, edu.example.work_department.website.Question), call(), call([Ljava.lang.Object;), call(java.lang.Object), call(edu.example.work_department.website.Answer, edu.example.work_department.website.Question), findAll()
The following classes appear as argument class and as parameter class, but are defined by different class loader:
edu.example.work_department.website.Answer (defined by 'java.net.URLClassLoader@689319a1' and 'groovy.lang.GroovyClassLoader@8db6e40'), edu.example.work_department.website.Question (defined by 'java.net.URLClassLoader@689319a1' and 'groovy.lang.GroovyClassLoader@8db6e40')
If one of the method suggestions matches the method you wanted to call,
then check your class loader setup.. Stacktrace follows:
'groovy.lang.MissingMethodException: No signature of method: edu.example.work_department.website.Question$__clinit__closure1_closure3.doCall() is applicable for argument types: (edu.example.work_department.website.Answer, edu.example.work_department.website.Question) values: [Answer: Answer 2, edu.example.work_department.website.Question : 1]
Possible solutions: doCall(edu.example.work_department.website.Answer, edu.example.work_department.website.Question), call(), call([Ljava.lang.Object;), call(java.lang.Object), call(edu.example.work_department.website.Answer, edu.example.work_department.website.Question), findAll()
The following classes appear as argument class and as parameter class, but are defined by different class loader:
edu.example.work_department.website.Answer (defined by 'java.net.URLClassLoader@689319a1' and 'groovy.lang.GroovyClassLoader@8db6e40'), edu.example.work_department.website.Question (defined by 'java.net.URLClassLoader@689319a1' and 'groovy.lang.GroovyClassLoader@8db6e40')
If one of the method suggestions matches the method you wanted to call,

点击 questionInstance.save() 时发生错误命令。我做错了什么,它不喜欢我的参数类型?

最佳答案

最好避免数据绑定(bind)

object.properties = params

除此之外,您还可以使用命令类 grails command class creation

您可以使用命令类对象绑定(bind)数据。

对于您的域类 Answer
创建命令类为:
Class AnswerCommand {      

String body
Integer ordinal
String reason

static constraints = {
body blank: false
ordinal unique: 'question'
}
}

并将参数读取为命令类对象。
def update(Long id, Long version, AnswerCommand answerCmd) {

def questionInstance = Question.get(id)

//questionInstance.properties = params // use the below codes.

questionInstance.body = answerCmd.body
questionInstance.ordinal = answerCmd.ordinal
questionInstance.reason = answerCmd.reason

//here you can use bindData() also. But this is for making you a clear picture.


if (!questionInstance.save(flush: true)) {
render(view: "edit", model: [questionInstance: questionInstance])
return
}

redirect(action: "index", id: questionInstance.id)
}

关于hibernate - 麻烦将参数绑定(bind)到对象并成功保存(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25413337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com