本文介绍了java.lang.NumberFormatException:对于输入字符串:“id”对于Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想从控制器传递连接查询结果来查看,但我得到错误java.lang.NumberFormatException:对于输入字符串:id 我已经经历了重复的问题并将解决方案应用于我的场景,但未取得成功。 重复问题-1 重复问题-2 重复问题-3 DAO code public List< Object []> showEmployee_Role(){ try { 会话会话= HibernateUtil.getSessionFactory()。o penSession(); SQLQuery query = session.createSQLQuery(select e.id as eid,r.id as rid,e.ename,e.enumber,r.description from employee_profile e INNER JOIN role r ON(e.role_id = r 。ID)); List< Object []> employee_role = query.list(); return employee_role; } catch(Exception e){ System.out.println(显示Employee_Role时出错+ e.getMessage()); } 返回null; $ b $ p $控制器代码 List< Object []> showEmployee_role = employeeDaoImpl.showEmployee_Role(); request.setAttribute(employeeList,showEmployee_role); RequestDispatcher rd = request.getRequestDispatcher(EmployeeView.jsp); rd.forward(request,response); 查看代码 < c:forEach items =$ {employeeList}var =emp> < tr> < td> $ {emp.id}< / td> < td> $ {emp.ename}< / td> < td> $ {emp.enumber}< / TD> < td> $ {emp.description}< / TD> < / tr> < / c:forEach> 员工实体 @Entity(name =employee_profile) public class Employee实现Serializable { private int id; 私人字符串ename; 私有字符串编号; 私人角色角色; 私人收藏<任务>任务; $ b公共职员(){ } 公共职员(字符串ename,字符串编号,角色角色){ this.ename = ename; this.enumber = enumber; this.role =角色; } @Id @GeneratedValue @Column(name =id) public int getId(){ return id; } public void setId(int id){ this.id = id; } public String getEname(){ return ename; } public void setEname(String ename){ this.ename = ename; } public String getEnumber(){ return enumber; } public void setEnumber(String enumber){ this.enumber = enumber; } @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name =role_id) public Role getRole(){ return角色; } public void setRole(角色角色){ this.role = role; $ b @OneToMany(cascade = CascadeType.ALL) @JoinTable( name =employee_task, joinColumns = @ JoinColumn(name =eid), inverseJoinColumns = @ JoinColumn(name =tid)) public Collection< Task> getTask(){返回任务; } public void setTask(Collection< Task> task){ this.task = task; $} b 角色实体 $ b @Id @GeneratedValue @Column(name =id) private int id; 私有字符串标题; 私有字符串描述; public int getId(){ return id; } public void setId(int id){ this.id = id; } public String getTitle(){ return title; } public void setTitle(String title){ this.title = title; } public String getDescription(){ return description; } public void setDescription(String description){ this.description = description; } } 堆栈轨迹 信息:Hibernate:select role0_.id as id1_2_,role0_.description as descript2_2_,role0_.title as title3_2_ from role0_ 信息:Hibernate:选择task0_.id作为id1_3_,task0_.tname作为tname2_3_从任务task0_ 信息:Hibernate:从employee_profile中选择employee0_.id作为col_0_0_ employee0_内部连接角色role1_在employee0_.role_id = role1_.id 信息:Hibernate:选择e.id作为eid,r.id作为rid,e.ename,e.enumber,r.description from employee_profile e INNER JOIN角色r ON(e.role_id = r.id) Info:Hibernate:select role0_.id as id1_2_,role0_.description as descript2_2_,role0_.title as title3_2_ from role0_ Info:Hibernate:select task0_.id as id1_3_,task0_.tname as tname2_3_ from task task0_ 警告:servlet jsp的Servlet.service()抛出异常 java.lang.NumberFormatException:对于输入字符串:id$ b $ java.util.Numb erFormatException.forInputString(NumberFormatException.java:65)在java.lang.Integer.parseInt(Integer.java:580)在java.lang.Integer.parseInt(Integer.java:615)。在javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:378)在javax.el.ArrayELResolver.getValue(ArrayELResolver.java:198)在javax.el.CompositeELResolver.getValue(CompositeELResolver .java:188) at com.sun.el.parser.AstValue.getValue(AstValue.java:140) at com.sun.el.parser.AstValue.getValue(AstValue.java:204 )在com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226 )在org.apache.jasper.runtime.PageContextImpl.evaluateExpression(PageContextImpl.java:1016 )。在org.apache.jsp.EmployeeView_jsp._jspx_meth_c_forEach_0(EmployeeView_jsp.java:297)处org.apache.jasper.runtime org.apache.jsp.EmployeeView_jsp._jspService(EmployeeView_jsp.java:125)。 HttpJspBase.service(HttpJspBase.java:111)位于javax.servlet.http.HttpServlet.service(HttpServlet.java:790) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473) at org。 apache.jasper.servlet.JspServlet.service(JspServlet.java:377)位于javax.servlet.http.HttpServlet.service(HttpServlet.java:790)位于org.apache.catalina.core。 StandardWrapper.service(StandardWrapper.java:1682) at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875) at org.apache.catalina.core.ApplicationDispatcher.invoke( ApplicationDispatcher.java:739) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575) at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:在org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428 546))在org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)$ b在Controller.Employe $ b eController.doPost(EmployeeController.java:214) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java: 790)在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)$ b在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) $ b在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)。在组织.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)在org.apache.catalina .core.StandardHostValve.invoke(StandardHostValve.java:174) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416) at org.apache.catalina.connector.CoyoteAdapter .service(CoyoteAdapter.java:283) at com.sun.enterprise.v3.services.impl.Conta inerMapper $ HttpHandlerCallable.call(ContainerMapper.java:459)在com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)。在org.glassfish.grizzly。 http.server.HttpHandler.runService(HttpHandler.java:206)位于org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)位于org.glassfish.grizzly。 http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)处org.glassfish.grizzly.filterchain org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119) .DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)在org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)在org.glassfish.grizzly.filterchain.DefaultFilterChain.execute (DefaultFilterChain.java:132) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecuto r.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy。的java:在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117 112))。在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56 )在org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:591 )在org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:571)$ b $在java.lang.Thread.run(Thread.java:748) 解决方案您可以修改您的Dao Class。 SQLQuery query = session.createSQLQuery(select e.id as eid,r.id从employee_profile e INNER JOIN角色r ON(e.role_id = r.id))删除,e.ename,e.enumber,r.description。 List< Object []> rows = query.list(); 列表<员工> employee_role = new ArralList<>(); for(Object [] row:rows){ Employee emp = new Employee(); 角色角色=新角色(); emp.setId(Integer.parseInteger(row [0] .toString())); role.setId(Integer.parseInteger(row [1] .toString())); emp.setEname(row [2] .toString()); emp.setEnumber(row [3] .toString())); role.setDescription(row [4] .toString())); emp.setRole(role); employee_role.add(emp); } return employee_role; list()SQLQuery类的方法 - 将始终返回您需要将类型转换为员工对象的对象列表。 这就是你的代码中缺少的东西。 在视图中,对于像rid这样的Role属性,Description - 可以请修改你的EL {emp.role.description} 和rid`相同I want to pass the join query results from controller to view , but I am gettingerror "java.lang.NumberFormatException: For input string: "id"I have already go through the duplicated questions and applied the solutions for my scenario but it is not getting successful.Duplicated Question -1 Duplicated Question -2 Duplicated Question -3DAO code public List<Object [] > showEmployee_Role(){ try { Session session=HibernateUtil.getSessionFactory().openSession(); SQLQuery query = session.createSQLQuery("select e.id as eid , r.id as rid , e.ename , e.enumber , r.description from employee_profile e INNER JOIN role r ON (e.role_id = r.id)"); List <Object []> employee_role =query.list(); return employee_role; } catch (Exception e) { System.out.println("Error in Show Employee_Role"+e.getMessage()); } return null;}Controller Code List<Object []>showEmployee_role=employeeDaoImpl.showEmployee_Role(); request.setAttribute("employeeList", showEmployee_role); RequestDispatcher rd = request.getRequestDispatcher("EmployeeView.jsp"); rd.forward(request, response);View Code <c:forEach items="${employeeList}" var="emp"> <tr> <td> ${emp.id} </td> <td> ${emp.ename} </td> <td> ${emp.enumber}</td> <td> ${emp.description}</td> </tr> </c:forEach>Employee Entity @Entity(name="employee_profile")public class Employee implements Serializable{private int id;private String ename;private String enumber;private Role role;private Collection <Task> task;public Employee() {}public Employee(String ename,String enumber,Role role){ this.ename=ename; this.enumber=enumber; this.role=role;}@Id @GeneratedValue@Column(name="id") public int getId() { return id;}public void setId(int id) { this.id = id;}public String getEname() { return ename;}public void setEname(String ename) { this.ename = ename;}public String getEnumber() { return enumber;}public void setEnumber(String enumber) { this.enumber = enumber;}@OneToOne(cascade = CascadeType.ALL)@JoinColumn(name = "role_id")public Role getRole() { return role;}public void setRole(Role role) { this.role = role;}@OneToMany(cascade=CascadeType.ALL)@JoinTable( name="employee_task", joinColumns=@JoinColumn(name="eid"), inverseJoinColumns=@JoinColumn(name="tid"))public Collection<Task> getTask() { return task;}public void setTask(Collection<Task> task) { this.task = task;}}Role Entity@Entity(name="role")public class Role implements Serializable{@Id @GeneratedValue@Column(name="id")private int id;private String title;private String description;public int getId() { return id;}public void setId(int id) { this.id = id;}public String getTitle() { return title;}public void setTitle(String title) { this.title = title;}public String getDescription() { return description;}public void setDescription(String description) { this.description = description;}}Stack traceInfo: Hibernate: select role0_.id as id1_2_, role0_.description as descript2_2_, role0_.title as title3_2_ from role role0_Info: Hibernate: select task0_.id as id1_3_, task0_.tname as tname2_3_ from task task0_Info: Hibernate: select employee0_.id as col_0_0_ from employee_profile employee0_ inner join role role1_ on employee0_.role_id=role1_.idInfo: Hibernate: select e.id as eid , r.id as rid , e.ename , e.enumber , r.description from employee_profile e INNER JOIN role r ON (e.role_id = r.id)Info: Hibernate: select role0_.id as id1_2_, role0_.description as descript2_2_, role0_.title as title3_2_ from role role0_Info: Hibernate: select task0_.id as id1_3_, task0_.tname as tname2_3_ from task task0_Warning: Servlet.service() for servlet jsp threw exceptionjava.lang.NumberFormatException: For input string: "id"at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:580)at java.lang.Integer.parseInt(Integer.java:615)at javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:378)at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:198)at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:188)at com.sun.el.parser.AstValue.getValue(AstValue.java:140)at com.sun.el.parser.AstValue.getValue(AstValue.java:204)at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)at org.apache.jasper.runtime.PageContextImpl.evaluateExpression(PageContextImpl.java:1016)at org.apache.jsp.EmployeeView_jsp._jspx_meth_c_forEach_0(EmployeeView_jsp.java:297)at org.apache.jsp.EmployeeView_jsp._jspService(EmployeeView_jsp.java:125)at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)at Controller.EmployeeController.doPost(EmployeeController.java:214)at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)at java.lang.Thread.run(Thread.java:748) 解决方案 Can you please Modify your Dao Class like this.SQLQuery query = session.createSQLQuery("select e.id as eid , r.id as rid , e.ename , e.enumber , r.description from employee_profile e INNER JOIN role r ON (e.role_id = r.id)"); List <Object []> rows =query.list(); List <Employee> employee_role = new ArralList<>();for(Object[] row : rows){ Employee emp = new Employee(); Role role = new Role(); emp.setId(Integer.parseInteger(row[0].toString())); role.setId(Integer.parseInteger(row[1].toString())); emp.setEname(row[2].toString()); emp.setEnumber(row[3].toString())); role.setDescription(row[4].toString())); emp.setRole(role); employee_role.add(emp); } return employee_role;"list()" method of SQLQuery class - will always return the list of Objects that you need to type cast to Employee Object.This is what is missing in your code.In view , For Role attributes like rid , Description -can you please modify your EL like {emp.role.description} and same for rid` 这篇关于java.lang.NumberFormatException:对于输入字符串:“id”对于Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 21:51