我想将联接查询结果从控制器传递到view,但是出现错误“ java.lang.NumberFormatException:对于输入字符串:“ id”
DAO代码
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; }
控制器代码
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 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; }}
角色实体
@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; } }
堆栈跟踪
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_ Info: Hibernate: select employee0_.id as col_0_0_ from employee_profile employee0_ inner join role role1_ on employee0_.role_id=role1_.id Info: 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 exception java.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)
您可以这样修改您的Dao类吗?
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;
SQLQuery类的 “ list()”方法 -始终返回您需要将类型转换为Employee Object的Object列表。这是您的代码中缺少的内容。
在视图中,对于rid属性,Description等角色属性-您能{emp.role.description}为rid 修改EL一样吗?
{emp.role.description}