gpt4 book ai didi

java - 运行 HQL 时发生类转换异常

转载 作者:行者123 更新时间:2023-12-01 23:18:57 25 4
gpt4 key购买 nike

我有我的 Pojo 类(class):

@Entity
@Table(name="INSTITUTE")
public class Institute {
private int id;
private Member member_id;
private String yearoffrom;
private Date created_date;
private Country country_id;
private State state_id;
private City city_id;
private String josh_rating;
private String institute_type;
private Clob about;
private String tags;
private String natureofbus;
private String placement;
private String placement_percentage;
private String affiliations;
private String scholarship;
private String keyword;
private String avg_salary;
private String document_path;
private String contact_person;
private String address;
private String source;
private String zip;
private int is_active;
private int is_deleted;
private AdminUser created_by_id;
private AdminUser verified_by_id;

我通过 HQL 仅选择几列

select inst.id,inst.member_id.name as institutename,inst.institute_type,inst.city_id.name as cityname,inst.state_id.name as statename,inst.country_id.name as countryname,inst.created_by_id.username as createdby,inst.verified_by_id.username as verifiedby  from Institute inst

在运行代码时,我发现 Hibernate 查询是由应用程序生成的,并且正在返回结果集。但是当我想通过这段代码遍历列表时:

for(Institute bean : institutelist) {

应用程序抛出 ClassCastException。

如何解决这个问题?

以下是跟踪:

SEVERE: Servlet.service() for servlet [spring] in context with path [/EducationNew] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to education.bean.Institute] with root cause
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to education.bean.Institute
at education.service.InstituteListingService.getInstituteList_HQL(InstituteListingService.java:45)
at education.controller.LoginController.validateUser(LoginController.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

HQL查询: hibernate :select * from ( select institute0_.id as col_0_0_, member1_.name as col_1_0_, institute0_.institute_type as col_2_0_, city2_.name as col_3_0_, state3_.name as col_4_0_, country4_.name as col_5_0_, adminuser5_.username as col_6_0_, adminuser6_.username as col_7_0_ from INSTITUTE institute0_, MEMBER member1_, City city2_, State state3_, Country country4_, AdminUser adminuser5_, AdminUser adminuser6_ where institute0_.member_id=member1_.id and institute0_.city_id=city2_.id and institute0_.state_id=state3_.id and institute0_.country_id=country4_.id and institute0_.created_by_id=adminuser5_.id and institute0_.verified_by_id=adminuser6_.id ) where rownum <= ?

最佳答案

您的查询当前返回的对象结果列表不是 Institute 的实例 - 由于 inst.member_id.name 等别名作为 Institutename、选择成员(member)名称而不是成员(member)本身等。

如何解决这个问题?解决方案很少,但我会尝试将学院选择为新学院:

Query query = session.createQuery("Select new Institute(inst.id, inst.name, inst.stuff...) from Institute inst");

完整示例可以在 here 找到

其他解决方案是使用 Hibernate projections .

关于java - 运行 HQL 时发生类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20832057/

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