代码片段有太多错误,我会尽力帮助您理解如何以更好的方式将所有上述内容写入,以令人困惑的方式编写内容isn不会让你更容易调试:
// You are createing a list in which you query Employee.findAll
List<Employee> employee=Employee.findAllById(session.getAttribute("empID"))
//But inside this list you are only looking for that 1 id.
上面的列表将总是包含1个元素。您正在使用的,如果你知道ID真的不需要findById你可以做
Employee.get(session.getAttribute("empID") as Long)
//You are then creating employeeMilestone
EmployeeMilestone employeeMilestone=new EmployeeMilestone()
//Then without checking your 1 list result you are directly attempting to get the first element
employeeMilestone.setEmployee(employee.get(0))
让我们再次尝试:
def save(){
def milestone=new Milestone(params)
milestone.save()
//Do you even have something in your session?
if (session.getAttribute("empID")) {
println "yes we have ${session.getAttribute("empID")}"
Employee employee=Employee.get(session.getAttribute("empID") as Long)
if (employee) {
println "yes we have eployee ${employee?.id}
EmployeeMilestone employeeMilestone=new EmployeeMilestone()
employeeMilestone.employee=eployee
employeeMilestone.milestone=milestone
employeeMilestone.save()
redirect(action: "show",id: employeeMilestone.id)
} else {
println "No employee found"
}
} else {
println "session could not be found"
}
}
希望这不会使其向的第四尝试你问一些显然需要很长时间才能下沉的东西
你在动作中重定向代码的事实表明你正在控制器中执行上述所有操作,这是更进一步的错误转向。
在服务器中做事务性的东西在控制器中做控制器的东西,并查看东西的视图。
可能的重复[关于将数据保存到grails数据库](https://stackoverflow.com/questions/45095609/about-saving-data-into-grails-databse) – Daniel
这只是https:// stackoverflow.com/questions/45095609/about-saving-data-into-grails-databse你已经接受了答案。 – Daniel
正确地格式化文本 – litelite