2

I have started using Spring MVC and hibernate few days back and I am facing one error.

My Entity Class

@Entity
@Table(name="TESTERS")
public class Questioner {

    @Id

    @Column(name="FIRSTNAME")
    private String firstname;

    @Column(name="LASTNAME")
    private String lastname;



    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }


    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }




}

My DAOImpl Query

public List<Questioner> listTester() { 
    return sessionFactory.getCurrentSession().createQuery("" +
                    "SELECT t.firstname,t.lastname FROM Questioner t,Program p " +
                    "where t.programid=p.programid ").list(); 
}

My Controller for getting this data and passing to jsp file

@RequestMapping(value = "/tester")
public String listContacts(Map<String, Object> map)
{
map.put("testerTableGet", testerservice.listTester());
return "tester";
}

My JSP File where I am displaying this data

<c:forEach items="${testerTableGet}" var="tester">
<tr>
<td>${tester.lastname}, ${tester.firstname}</td>
</tr>
</c:forEach>

The query is working fine and it also getting the data needed from database , also my Questioner and Program Class has correct mapping from the database include datatype.

But while executing I am getting below error.

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause
java.lang.NumberFormatException: For input string: "lastname"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:492)
java.lang.Integer.parseInt(Integer.java:527)
javax.el.ArrayELResolver.coerce(ArrayELResolver.java:166)
javax.el.ArrayELResolver.getValue(ArrayELResolver.java:46)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
org.apache.el.parser.AstValue.getValue(AstValue.java:183)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
org.apache.jsp.WEB_002dINF.views.tester_jsp._jspx_meth_c_005fforEach_005f4(tester_jsp.java:387)
org.apache.jsp.WEB_002dINF.views.tester_jsp._jspService(tester_jsp.java:223)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Can anybody help ?

Thanks.

7
  • There should not be comma before FROM in SELECT t.firstname,t.lastname,FROM Questioner Commented Jun 30, 2013 at 17:27
  • Oh Sorry it's typo mistake while posting in real its working. I have edit it Commented Jun 30, 2013 at 17:29
  • 1
    Post the full stack trace, so that we know where this exception comes from. Also, it seems you missed the part about associations between entities in the manual. This is an extremely important part. You shouldn't have IDs to other entities in your entities, but associations between them. Commented Jun 30, 2013 at 17:30
  • Please also specify what is class you return from DAO Commented Jun 30, 2013 at 17:31
  • @WandMaker Hey I have include class where I am return DAO Please see the original post Commented Jun 30, 2013 at 17:44

1 Answer 1

3

The query above returns a List<Object[]>. So, in your JSP, inside forEach, the tester variable references an Object[]. And Object[] dorsn't have any lastname (or firstname) property.

You're missing a loop which transforms a loop that transforms the List<Object[]> into a List<User> before placing the list into the model.

Or you should change the query to

SELECT t FROM Questioner t,Program p ...

which would return a List<Questioner> instead of a List<Object[]>.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok. Can you let know how to transforms the List<Object[]> into a List<User> ? Or what if I have use List<Object[]> in my JSP page. because my query will have result something like t.firstname,t.lastname,p.programid So it will not have only List<User> .
You will have to define relation between Questioner and Program in your Entity, I guess by making a member attribute in Questioner of type Program. Also, use JPA Query and not SQL query.
Create a class containing the fields that your query retrieves, the loop over the List<Object[]>, and at each iteration, create an instance of this class.
Ok, I am sorry then. My mistake. Generally, the FROM clause will have only entity listed in JPA QL so that the return values are of that type.
@JBNizet hey I found another way where I created one view with all the data that I want and then create class for that view and fetch it. What do you is this efficient way of doing ? and by the way it's working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.