Domanda

I had my 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;

I am selecting only few columns through 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

On running the code, I found that Hibernate query is generated by Application and is returning the resultset. But when I want to traverse the list by this code:

for(Institute bean : institutelist) {

Application throws an ClassCastException.

How to resolve this problem?

Following is the 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 Query: 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 <= ?

È stato utile?

Soluzione

Your query currently returns you a result list of objects which aren't instances of Institute - due to aliases like inst.member_id.name as institutename, selecting Member name instead of Member itself etc.

How to fix that? There are few solutions, but I would try to selecting Institute into new Institute:

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

Full example can be found at here

Other solution is to use Hibernate projections.

Altri suggerimenti

If you are selectively selecting some attributes instead of selecting the whole entity, the return is in fact an list of tuple (List<Object[]>). Each entry in the list (which is an Object[]) represent one row of result.

Therefore your loop should look like

List<Object[]> results = (List<Object[]>) yourQuery.list();
for (Object[] result : results) {
    //....
}

Quoted from http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch16.html#queryhql-select :

Queries can return multiple objects and/or properties as an array of type Object[]:

select mother, offspr, mate.name from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr

More way to handle selecting selective properties is also described in the quoted link

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top