关于将数据保存到grails数据库 [英] About saving data into grails databse

查看:224
本文介绍了关于将数据保存到grails数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  def save(){
List<

这是我的项目代码,我想将我的数据保存到数据库中。 ;员工> list = Employee.findAllById(session.getAttribute(empId))
里程碑里程碑=新里程碑()
milestone.setMilestone_Date(params.milestone_Date)
milestone.setMilestone_Name(params.milestone_Name)
milestone.setMilestone_Description(params.milestone_Description)
milestone.save()
EmployeeMilestone employeeMilestone = new EmployeeMilestone()
Employee employee = list.get(0)
employeeMilestone .setEmployee(employee)
employeeMilestone.setMilestone(里程碑)
employeeMilestone.save()
[employeeMilestones:employeeMilestone]
}

出现此错误


错误500:内部服务器错误URI / ProjectTrackerMain / milestone / save类java.lang.IndexOutOfBoundsException消息索引:0,大小:0



解决方案

你实际上没有提出问题,所以这可能有点含糊!



当您尝试访问某个位置中没有某些东西的集合中的内容时,会发生IndexOutOfBoundsException。例如,也许你要求列表中的第十个元素,但只有两个。在你的情况下,你要求在这行代码中使用第零个元素(简单的英语First):

  Employee employee = list.get(0)

大概这个列表是空的。您的错误消息显示大小:0。您无法从列表中的第一个元素中获取零个元素,因此这是索引超出界限的例外。



为什么您的列表为0?这是一个不同的问题。您可以尝试打印出来。

  session.getAttribute(empId)

查看您的员工ID是否符合您的预期。您也可以查看数据库中的数据,以查看您是否真的设法保存员工!无论如何,你没有得到你所期望的数据,然后你正在尝试使用它。



通常,使用调试器来查看你的元素,或者只是使用println来查看值有助于调试这样的问题。这样,你会在第一行发现你的雇员清单并不是你所期望的,而不是你以后尝试使用它的几行!


This my project code I want to save my data into database.

def save(){
    List<Employee> list = Employee.findAllById(session.getAttribute("empId"))
    Milestone milestone = new Milestone()
    milestone.setMilestone_Date(params.milestone_Date)
    milestone.setMilestone_Name(params.milestone_Name)
    milestone.setMilestone_Description(params.milestone_Description)
    milestone.save()
    EmployeeMilestone employeeMilestone=new EmployeeMilestone()
    Employee employee = list.get(0)
    employeeMilestone.setEmployee(employee)
    employeeMilestone.setMilestone(milestone)
    employeeMilestone.save()
    [employeeMilestones:employeeMilestone]
}

I am getting this error

Error 500: Internal Server Error URI /ProjectTrackerMain/milestone/save Class java.lang.IndexOutOfBoundsException Message Index: 0, Size: 0

解决方案

You didn't actually ask a question, so this may be a bit vague!

An IndexOutOfBoundsException happens when you try to access something from a collection in a location where there is no "something". For example, maybe you asked for the tenth element in a list, but there are only two. In your case, you're asking for the zeroth (in plain English, "First") element on this line of code:

Employee employee = list.get(0)

and presumably the list is empty. Your error message says "Size: 0". You can't get the first element from a list that has zero elements in it, so that's an index out of bounds exception.

Why is your list 0? That's a different question. You might try printing out

session.getAttribute("empId")

to see if your employee ID is what you expected. You might also look at the data in your database to see if you actually managed to save an employee! One way or another, you're not getting the data you expected, and then you're trying to use it.

In general, using a debugger to look at your elements, or just using "println" along the way to look at values is helpful in debugging problems like this. That way, you'll find out on line 1 that your list of Employees is not what you expected, instead of several lines later when you try to use it!

这篇关于关于将数据保存到grails数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆