I'm working w/ a simple sample application, just trying to figure out Hibernate and evaluating it for my company, to see if we'll use it in production code or not.
I've had one hell of a day w/ it so far and for the last twelve hours...can't seem to get around the simplest of things. I've spent half the day googling and search these forums and don't see anything along the lines of what I'm up against.
I'm sure I'm just screwing up the Session somehow and not using it right but here's what I've got...
-------------------------------------------------------------------
I created a small Hibernate-based app which maps a single class to a single table.
Here is the class:
Code:
package com.scientifik.hibernate;
import java.util.Date;
public class Event
{
private String title;
private Date date;
private Long id;
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
}
Here is my mapping file:
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">
<hibernate-mapping>
<class name="com.scientifik.hibernate.Event" table="Event">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>
<property name="date" column="timestamp"/>
<property name="title" column="eventtitle"/>
</class>
</hibernate-mapping>
Here is my 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">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost/hibernate</property>
<property name="connection.username">postgres</property>
<property name="connection.password"></property>
<property name="connection.pool_size">5</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/scientifik/hibernate/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The table matches in Postgres and when I first built & ran this app, it worked great. I was inserting records, selecting them, the works...I was really impressed.
Then, I added a *second* class and a second table. The class was named User and contained just a few getters/setters just like the Event class listed above...nothing special about it. I created the User.hbm.xml file to match the table and added this line to the hibernate.cfg.xml file:
Code:
<mapping source="com/scientifik/hibernate/User.hbm.xml"/>
I put it just below the tag that maps to the Event.hbm.xml file.
I manually added a few records to my database and then tried to select them out w/ Hibernate and now I get this error:
Code:
java.lang.NoClassDefFoundError
com.scientifik.hibernate.Hibernate.listEvents(Hibernate.java:83)
com.scientifik.hibernate.Hibernate.doGet(Hibernate.java:57)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Hibernate.java line 83 is where I'm calling the Session from Hibernate (i.e. Session s = HibernateUtil.getCurrentSession();)
When stepping through the code is gets to that line and simply sits there...and about 20 seconds later it throws the exception listed above.
So, as a test...I removed the mapping file, the reference to the class in hibernate.cfg.xml, and even deleted the class itself. I cleaned and re-compiled the app and rebooted Tomcat.
Lo and behold, I get the same error calling the original code (trying to insert/list Event records)!!!!
I can't do what I could ORIGINALLY do...I'm getting the NoClassDefFoundError for the Event class!!
The Event class, Event.hbm.xml, and the reference (mapping source tag) to it in hibernate.cfg.xml are right where they should be...just as they were before I added the second class!!
The HibernateUtil class I'm using came from the Caveat Emptor application and here is the source (remember, getCurrentSession() is where my app fails now):
It's a big file, if you want to see it, download from
http://www.zambizzi.net/HibernateUtil.javaI have not altered a single *line* in this code, I downloaded it, used it for a sample app w/ a single class successfully, and though it would be alright.
Here's an example of how I'm calling this HibernateUtil class to select some records out of the database, which worked until I added the second class to the project:
Code:
Session session = HibernateUtil.getCurrentSession();
HibernateUtil.beginTransaction();
List<Event> result = session.createQuery("from Event").list();
try
{
for (Event e : result)
{
response.getWriter().println(e.getTitle() + "<br />");
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
Some details, just for the record, I'm using:
JDK 1.5 (this is an absolute requirement)
Eclipse 3.1 + Webtools
Hibernate 3.0.5
PostgreSQL 8 + latest official JDBC driver
Tomcat 5.5.9 (also tested w/ same results using Resin 3.0.14)
Tested on both Windows and Linux on both Tomcat 5.5.9 and Resin 3.0.14 w/ the EXACT same results.
Obviously I did this in a servlet but I also copied the same code into a console application and got the exact same error. Hibernate is the problem and I assume I'm using it wrong...but I'd like someone to show me what's up and how I can get around this.
I copied this same code to another machine at work today and was able to re-create the problem, step-by-step.
It is NOT an issue w/ my CLASSPATH variable unless Hibernate did something to alter it that I just don't understand!
Please help, I'm COMPLETELY at wits-end and ready to just suffer it out w/ JDBC in order to get rolling on some new code vs. puttering around for hours-on-end!
If this wasn't a comprehensive enough post, I don't know what else to provide!!
Thank you!
-v