facebook

[Closed] ClassCastException Hibernate

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #247001 Reply

    Russ
    Member

    I have a simple application using a jsp to present information. It works through Hibernate to a MySQL database. I am using Eclipse 3.1.1, MyEclipse workbench 4.1 MySQL 5.0, and Hibernate 3.0 The application is running on JBoss4.0.3SP1 This error is taking place when I take the results and try to place it in the session.

    What I want to know is retreiving the information from the database looks at though I am dealing with objects and not a collection of Catalog.class. When I go look at the current version of Hibernate 3.1, I can see several overloaded methods for createSQLQuery, yet I can not find them when I try to use them in my code. The only method avaialble to me is the one method with a String as a parameter for the SQL query. I would assume this is because of MyEclipse only using Hibernate3.0 and not Hibernate 3.1.

    How does one ensure Hibernate returns a collection of Catalog classes in the collection?

    In the JSP I am calling: AddJournal.jsp

    
      List catalogList = CatalogService.getInstance().getCatalogList();
      request.setAttribute("catalog", catalogList); <<< exception is happening here.....
    

    In the Hibernate portion: CatalogService

    
    public List getCatalogList(){
            
            Session session = null;
            try{
                 session = SessionFactory.currentSession();
                 // use lower case names for attritbutes and 
                 // substitute for table name
                 String SQL_QUERY ="SELECT c.catalogId,c.journal,c.publisher," + 
                 "c.date,c.title,c.author FROM Catalog c";
    
                 Query query = session.createQuery(SQL_QUERY);
                 for(Iterator it=query.iterate();it.hasNext();){
                   Object[] row = (Object[]) it.next();   
                   System.out.println("CatalogId: " + row[0]);
                   System.out.println("Journal: " + row[0]);
                   System.out.println("Publisher: " + row[1]);
                   System.out.println("Date: " + row[2]);
                   System.out.println("Title: " + row[3]);
                   System.out.println("Author: " + row[4]);
                 }//end for loop
                 query.setMaxResults(10);
                 return query.list();
            }catch (ObjectNotFoundException onfe){
                return null;
            }finally {
                if(session != null){
                    try{
                        session.close();
                    }catch (HibernateException e){
                        System.err.println("Hibernate Exception: "+ e.getMessage());
                        throw new RuntimeException(e);
                    }
                }
            }
        }//end method 
    

    When I run the application, I get this error:

    
    [09:17:33,828] [ERROR] [[jsp]] Servlet.service() for servlet jsp threw exception
    java.lang.ClassCastException: [Ljava.lang.Object;
        at org.apache.jsp.AddJournal_jsp._jspService(org.apache.jsp.AddJournal_jsp:150)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
        at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
        at java.lang.Thread.run(Thread.java:595)
    
    #247003 Reply

    Haris Peco
    Member

    russray,

    Your ‘getCatalogList’ return collection of Catalog (try debug or use generics).Your error isn’t related with hibernate.I suppose that you can’t set attribute of request to list.

    Best

    #247006 Reply

    Russ
    Member

    Take a look at the getCatalogList method.

    If I try to change the object from a Object[] to Catalog. I get the same problem–ClassCastException. This is where I make the deduction that the I was not getting a collection of Catalog objects back from Hibernate. Is this not what suppose to happen?

    #247009 Reply

    Haris Peco
    Member

    russray,

    I will try your problem today, but no immediately.Please, be patient.I answer you

    Thanks

    #247010 Reply

    Russ
    Member

    Okay, I figured it out.

    
    public List getCatalogList(){
            
            Session session = null;
            try{
                 session = SessionFactory.currentSession();
                 // use lower case names for attritbutes and 
                 // substitute for table name
                 String SQL_QUERY ="SELECT c.catalogId,c.journal,c.publisher," + 
                 "c.date,c.title,c.author FROM Catalog c";
                 List catalogList =  session.createCriteria(Catalog.class, SQL_QUERY).list();
                 for(Iterator it=catalogList.iterator();it.hasNext();){
                     Catalog cat = (Catalog) it.next();
                     System.out.println("CatalogId: " + cat.getCatalogId());
                     System.out.println("Journal: " + cat.getJournal());
                     System.out.println("Publisher: " + cat.getPublisher());
                     System.out.println("Date: " + cat.getDate());
                     System.out.println("Title: " + cat.getTitle());
                     System.out.println("Author: " + cat.getAuthor());
                   }//end for loop
                 
                 return catalogList;
            }catch (ObjectNotFoundException onfe){
                return null;
            }finally {
                if(session != null){
                    try{
                        session.close();
                    }catch (HibernateException e){
                        System.err.println("Hibernate Exception: "+ e.getMessage());
                        throw new RuntimeException(e);
                    }
                }
            }
        }//end method 
    
Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: [Closed] ClassCastException Hibernate

You must be logged in to post in the forum log in