-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem using session.find
PostPosted: Wed Mar 23, 2005 1:47 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 9:07 am
Posts: 27
Hi friends,

I have a very weird problem. I have a class called CDViewer.java which retrieves the details of all the CDs from the database. My problem is that when I do

List cds = session.find("from CD");

nothing happens. If you see the code below I have put

Code:
      System.out.println("11111111111111");
      List cds = session.find("from CD");
      System.out.println("22222222222222"); 


in the class CDViewer.java. Although "11111111" appears but "2222222222" donot get displayed and no exception is thrown. That means there is some problem in the line

List cds = session.find("from CD");

but I donot understand what the problem is. If I use any other session command (like List cds = session.createQuery("select * from cd").list();) the same thing happens.


Hibernate version: 2.0

Mapping documents:-CD.hbm.xml
--------------------------------------------------------------------
Code:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="com.CD" table="cd">
        <id name="id" type="int" unsaved-value="0">
            <column name="ID" sql-type="int" not-null="true"/>
            <generator class="increment"/>
        </id>
        <property name="title" column="TITLE"/>
        <property name="artist" column="ARTIST"/>
        <property name="purchaseDate" column="PURCHASEDATE"/>
        <property name="cost" column="COST"/>
    </class>
</hibernate-mapping>

--------------------------------------------------------------------

Code between sessionFactory.openSession() and session.close():
---------------------------------------------------------------------------------
Class CDViewer.java

Code:
package com;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import java.util.*;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class CDViewer extends HttpServlet {
  private SessionFactory sessionFactory;

  public void init(ServletConfig config) throws ServletException {
    super.init(config);
   
    try {
      sessionFactory = new Configuration().configure().addClass(com.CD.class).buildSessionFactory();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void displayAll(PrintWriter out, Session session) {
    try {
      out.println("<html>");
      out.println("<table border='1'>");
      out.println("<tr><td>Title</td><td>Artist</td><td>cost</td></tr>");

System.out.println("11111111111111");
List cds = session.find("from CD");
System.out.println("22222222222222");


Code:
      Iterator iter = cds.iterator();
      while (iter.hasNext()) {
       CD cd = (CD)iter.next();
        out.println("<tr><td>");
       out.println(cd.getTitle());
        out.println("</td><td>");
     out.println(cd.getArtist());
        out.println("</td><td>");
        out.println(cd.getCost());
     out.println("</td></tr>");
      }        
    } catch(Exception e) {}

    out.println("</table>");   
    out.println("</html>");
  }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
                        throws IOException, ServletException {

    response.setContentType("text/html");
    PrintWriter out = response. getWriter();

        Session session = null;

        try {
          session = sessionFactory.openSession();
          String action = request.getParameter("submit");
          if (action.equals("Pull All CDs")) {
            displayAll(out, session);
          } else {
             out.println("Bad Input");
          }

          session.flush();
          session.close();

        } catch (Exception e) {
           e.printStackTrace();
        }
  }

  public void doPost(HttpServletRequest request,
                    HttpServletResponse response)
                        throws IOException, ServletException {
    doGet(request, response);
  }
}

---------------------------------------------------------------------------------

Name and version of the database you are using:
---------------------------------------------------------------------------------
Oracle 9i
The table has the following fields-
ID,TITLE,ARTIST,PURCHASEDATE,COST
---------------------------------------------------------------------------------


[c]hibernate.cfg.xml
---------------------------------------------------------------------------------
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@172.16.4.78:1521:prod</property>
        <property name="connection.username">abc</property>
        <property name="connection.password">abc</property>
        <property name="show_sql">true</property>
         <property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
        <mapping resource="CD.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

---------------------------------------------------------------------------------

Please help as I have been assigned a task and there is nobody I can ask.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 1:49 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 9:07 am
Posts: 27
I Forgot to add that below is my CD.java Model class


Code:
package com;

import java.io.*;
import java.util.*;

  public class CD {
    Integer id;
    String title;
    String artist;
    Date   purchaseDate;
    Double cost;

    public CD() {
    }

    public void setId(Integer id) {
      this.id = id;
    }

    public Integer getId(){
      return id;
    }

    public void setTitle(String title) {
      this.title = title;
    }

    public String getTitle() {
      return title;
    }

    public void setArtist(String artist) {
       this.artist = artist;
    }

    public String getArtist() {
      return artist;
    }

    public void setPurchaseDate(Date purchaseDate) {
      this.purchaseDate = purchaseDate;
    }

    public Date getPurchaseDate() {
      return purchaseDate;
    }

    public void setCost(Double cost) {
      this.cost = cost;
    }

    public Double getCost() {
      return cost;
    }
  }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 2:56 am 
Regular
Regular

Joined: Mon Feb 23, 2004 10:42 pm
Posts: 102
Location: Washington DC
1. You posted to the wrong forum. This forum is for Hibernate3.x

2. Try using
Code:
session.find("from com.CD");


3. Is the SQL being displayed in standard out?

4. Try turning on the debug for Hibernate to get a sense of where it is failing.

_________________
Matt Veitas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 4:43 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 9:07 am
Posts: 27
I tried using com.CD but that didn't help.

Could you please tell me how can i turn the debug option ON for Hibernate. Also what is the link for Hibernate 2 queries.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 4:47 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Why are you ignoring the exception? NEVER do this:

Code:
      Iterator iter = cds.iterator();
      while (iter.hasNext()) {
       CD cd = (CD)iter.next();
        out.println("<tr><td>");
       out.println(cd.getTitle());
        out.println("</td><td>");
      out.println(cd.getArtist());
        out.println("</td><td>");
        out.println(cd.getCost());
      out.println("</td></tr>");
      }       
    } catch(Exception e) {}


You should at the bare minimum be logging the exception.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 4:51 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
All of this looks like one of the typical examples from the "Professional Hibernate" book. It's a good idea if you don't continue reading it... and throw it away.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.