0

I want to pass the join query results from controller to view , but I am getting error "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 -3

DAO 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 trace

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)
progman
  • 376
  • 6
  • 15
  • 1
    At some point, your code is trying to convert the String ``"id"`` to a number. Take a look at the stacktrace to find out more. Or post it. Since i don't see that String anywhere: Might the "id" column in your database be a varchar and contain the value "id"? – f1sh Oct 20 '17 at 09:25
  • @f1sh I have updated with stack trace."id" is integer not a varchar. – progman Oct 20 '17 at 09:44
  • can you please try with "Integer" instead of int. – shivam Oct 20 '17 at 10:15
  • @shivam I'm getting same error stack after changing "Integer" instead of int. – progman Oct 20 '17 at 10:32
  • can you add the complete code of controller, dacause that's the your class the error occures at Controller.EmployeeController.doPost(EmployeeController.java:214) – Daniel Stiefel Oct 20 '17 at 11:19
  • @DanielStiefel why? The exception is thrown during evaluation of an EL expression. That's far away from the controller. – f1sh Oct 20 '17 at 11:21
  • @shivam why would that help? – f1sh Oct 20 '17 at 11:22
  • @f1sh ,SQLQuery method always returns List of Object[]. As you can see in DAO,we are getting result from SQLQuery and passing that to view, Now in View we are iterating the list and from every object we are accessing the attributes.but at that point there is no attribute in that object only thing we have in index. – shivam Oct 20 '17 at 11:36
  • @f1sh sorry, i think you are right, just saw the line - at Controller.EmployeeController.doPost(EmployeeController.java:214) - in stacktrace – Daniel Stiefel Oct 20 '17 at 11:39

3 Answers3

0

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`

shivam
  • 489
  • 2
  • 8
  • 22
0

Each row in your sql query result is an array as you can see in you declaration:

List <Object []> employee_role =query.list();

In your jsp, you try to access each array's contents using a property name, which you don't have. Instead, you have arrays, which means you have to access the content with an index. Try:

 <td> ${emp[0]} </td> 
 <td> ${emp[1]} </td>

and so on.

The exception is caused by the EL evaluator (see the stacktrace: javax.el.ArrayELResolver.getValue) that tries to treat the property name as in index of the emp array, attempting to convert it to an Tnteger. Of course that doesnt work for the String "id".

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • @flsh I'm getting error "javax.el.PropertyNotFoundException: The class 'java.lang.Integer' does not have the property 'id'. – progman Oct 20 '17 at 11:45
  • that sounds like you changed it to ``${emp[0].id}``. Dont do that. – f1sh Oct 20 '17 at 12:37
0

According to the documentation of SQLQuery. The values in the list are Object[] array. Could you please try emp[0] for id value and similarly for ename-emp[2],enumber - emp[3],description - emp[4]