-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to Return a Collection of Domain Objects?
PostPosted: Sun Feb 19, 2006 12:14 pm 
Beginner
Beginner

Joined: Thu Dec 08, 2005 12:18 pm
Posts: 21
Location: Birmingham, Alabama
I have a simple application using a jsp to presenting 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 as 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 Hibernate3.0 returns a collection of Catalog classes in the collection?



In the JSP I am calling: AddJournal.jsp
Code:
  List catalogList = CatalogService.getInstance().getCatalogList();
  request.setAttribute("catalog", catalogList); <<< exception is happening here.....



Hibernate version: 3.0

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="com.rrcorp.hibernate.Catalog" table="catalog">
        <id name="catalogId" type="integer">
            <column name="CatalogId" />
            <generator class="native"></generator>
        </id>
        <property name="journal" type="string">
            <column name="Journal" length="25" />
        </property>
        <property name="publisher" type="string">
            <column name="Publisher" length="25" />
        </property>
        <property name="date" type="string">
            <column name="Date" length="25" />
        </property>
        <property name="title" type="string">
            <column name="Title" length="45" />
        </property>
        <property name="author" type="string">
            <column name="Author" length="25" />
        </property>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

CatalogService
Code:
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



Full stack trace of any exception that occurs:

Code:
[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)


Name and version of the database you are using:
Code:
MySQL 5.0


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 3:31 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
If you want to retrieve all catalogs, your getCatalogList() method should look like this :

Code:
public List getCatalogList(){
     
      Session session = null;
       try{
           session = SessionFactory.currentSession();
           // use lower case names for attritbutes and
           // substitute for table name
           String HQL_QUERY ="FROM Catalog";

           Query query = session.createQuery(HQL_QUERY);
           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


This way you'll get a collection of Catalog, not Object


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 3:40 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
Make sure you're looking at org.hibernate.Session and not org.hibernate.classic.Session. The classic Session does have more createSqlQuery methods, but they're all deprecated in favor of org.hibernate.Session.
Old classic Session javadoc: http://www.hibernate.org/hib_docs/v3/api/org/hibernate/classic/Session.html
New Session javadoc: http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html

I would take some steps to verify that the List you get back contains Catalog objects. Instead of returning query.list() in CatalogService.getCatalogList(), try this:

Code:
List catalogList = query.list();
Iterator iterator = catalogList.iterator();
while (iterator.hasNext())
{
    Object nextObject = iterator.next();
    Catalog nextCatalog = (Catalog)nextObject;
    //call a catalog specific method on nextCatalog
    //and see if you get an error
}
return catalogList;


If the Object nextObject = iterator.next() line works but the following ones throw a class cast exception, then your problem is with your query or your mapping files, etc. If it all works without error, then it is returning a List of Catalogs, and your problem is likely in the JSP.

Sorry, this probably won't directly solve the problem, but it might provide some new info that could help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 3:42 pm 
Beginner
Beginner

Joined: Sun Feb 19, 2006 3:50 am
Posts: 34
Ah, never mind my post, the-gtm's got the solution. That's what I get for not working directly with HSQL.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.