我参加了我的Pojo课程:
@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查询是由Application生成的,并返回结果集。但是当我想通过这段代码遍历列表时:
for(Institute bean : institutelist) {
Application抛出ClassCastException。
如何解决此问题?
以下是stracktrace:
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查询:
休眠: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 <= ?
答案 0 :(得分:2)
您的查询当前返回的是不属于Institute实例的对象的结果列表 - 由于像inst.member_id.name这样的别名作为学院名称,选择成员名称而不是成员本身等。
如何解决这个问题?解决方案很少,但我会尝试将研究所选入新研究所:
Query query = session.createQuery("Select new Institute(inst.id, inst.name, inst.stuff...) from Institute inst");
可在here
找到完整示例其他解决方案是使用Hibernate projections。
答案 1 :(得分:1)
如果您有选择地选择某些属性而不是选择整个实体,则返回实际上是元组(List<Object[]>
)的列表。列表中的每个条目(Object[]
)代表一行结果。
因此你的循环应该看起来像
List<Object[]> results = (List<Object[]>) yourQuery.list();
for (Object[] result : results) {
//....
}
引自http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch16.html#queryhql-select:
查询可以将多个对象和/或属性作为数组返回 输入Object []:
select mother, offspr, mate.name from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr
引用链接
中还介绍了处理选择性选择属性的更多方法