0

I am getting the following error "Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity" when trying to fetch the list of mechanic rows from database and pass to the jsp page.

Mechanic Entity

@Entity
@Table(name="Mechanic")
public class MechanicEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)

int mechanicId;
String mechanicType;
int numberOfVehicles;
public int getMechanicId() {
    return mechanicId;
}
public void setMechanicId(int mechanicId) {
    this.mechanicId = mechanicId;
}
public String getMechanicType() {
    return mechanicType;
}
public void setMechanicType(String mechanicType) {
    this.mechanicType = mechanicType;
}
public int getNumberOfVehicles() {
    return numberOfVehicles;
}
public void setNumberOfVehicles(int numberOfVehicles) {
    this.numberOfVehicles = numberOfVehicles;
}

}

RegisterController.java

...
RegisterService service;
 @ModelAttribute("MechList")
 public Map<Integer,Integer> populateMechId()throws Exception {

    List<Mechanic> allMech = service.getAllMechanic();
    Map<Integer,Integer> MechanicMap=new HashMap<Integer,Integer>();

    for(Mechanic mechanic:allMech){
        MechanicMap.put(mechanic.getMechanicId(),mechanic.getMechanicId());
    }

    return MechanicMap;
}  ...

RegisterService.java

...
RegisterDAO dao;
public List<Mechanic> getAllMechanic() throws Exception
{
  return dao.getAllMechanic();
}
...

RegisterDAO.java

public List<Mechanic> getAllMechanic() throws Exception
{
    List<Mechanic> mlist=new ArrayList<Mechanic>();
    Query q=em.createQuery("select m.mechanicId,m.mechanicType from MechanicEntity m");
    List<MechanicEntity> l1=q.getResultList();
    for(MechanicEntity m:l1)
    {
        Mechanic mechanic=new Mechanic();
        mechanic.setMechanicId(m.getMechanicId());
        mlist.add(mechanic);

    }
    return mlist;
}

registerVehicle.jsp

<form:form name="registerForm1"  action="registerVehicle.htm"  modelAttribute="serviceregister" method="GET" >
<form:select path="mechanicId">
<form:options items="${MechList}" />
</form:select>
</form:form>

Error:

Type Exception Report

Message org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception 
org.apache.jasper.JasperException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:594)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:495)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



Root Cause 
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:750)
    org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:720)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:92)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



Root Cause 
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.entity.MechanicEntity
    com.dao.RegisterDAO.getAllMechanic(RegisterDAO.java:19)
    com.service.RegisterService.getAllMechanic(RegisterService.java:27)
    com.controller.RegisterController.populateMechId(RegisterController.java:40)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:163)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:750)
    org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:720)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:92)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Please help me rectify it, so the dropdown is displayed when the view loads.

2
  • Have a look on this question, maybe it helps you. You are not fetching MechanicEntity but some DTO or projection with two fields from MechanicEntity. Commented Nov 24, 2018 at 20:58
  • Is there any need to have MechanicEntity and Mechanic classes? Only MechanicEntity seems enough for me. Commented Nov 24, 2018 at 21:05

1 Answer 1

1

Your code doesn't even compile because of:

Query q=em.createQuery("select m.mechanicId,m.mechanicType from MechanicEntity m");
List<MechanicEntity> l1=q.getResultList();

The problem is that getResultList() return a List<Object> instead of List< MechanicEntity>.

To make it work:

 List<MechanicEntity> l1 = createQuery("select m from MechanicEntity m", MechanicEntity.class).getResultList();
Sign up to request clarification or add additional context in comments.

2 Comments

I made the changes and now it shows, "java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [com.entity.MechanicEntity] "
@Sidzler See my updated answer. Your query was wrong.

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.