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.