gpt4 book ai didi

java - Ljava.lang.Object;无法转换为 com.entity.MechanicEntity

转载 作者:行者123 更新时间:2023-12-02 10:33:35 24 4
gpt4 key购买 nike

尝试从数据库获取机械行列表并将其传递到 jsp 页面时,出现以下错误“Ljava.lang.Object;无法转换为 com.entity.MechanicEntity”。

机械实体

@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>

错误:

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)

请帮我纠正它,以便在加载 View 时显示下拉列表。

最佳答案

您的代码甚至无法编译,因为:

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

问题是 getResultList()返回 List<Object>而不是List< MechanicEntity> .

要使其正常工作:

 List<MechanicEntity> l1 = createQuery("select m from MechanicEntity m", MechanicEntity.class).getResultList();

关于java - Ljava.lang.Object;无法转换为 com.entity.MechanicEntity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53462233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com