-->
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.  [ 7 posts ] 
Author Message
 Post subject: Can't recover from exception...
PostPosted: Fri Sep 09, 2005 9:37 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
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.java

I 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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 11:00 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What about a DEBUG log?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 11:14 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
christian wrote:
What about a DEBUG log?


Don't have one, what can I do to get you one?

If it isn't already obvious, I'm *very* new to Hibernate and somewhat new to Java altogether (< 1 year).

I'm from a MS background (asp, COM, .NET, C#, etc.) - and after years of that...am migrating to Java professionally.

Thanks!


Top
 Profile  
 
 Post subject: small revision
PostPosted: Fri Sep 09, 2005 11:20 pm 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
I forgot to mention one thing, which may be huge.

The FIRST time I run the app, I see this exception:

Code:
java.lang.ExceptionInInitializerError
   com.scientifik.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:99)


HibernateUtil.java Line 99 is in bold below:

Code:
    static {
        // Create the initial SessionFactory from the default configuration files
        try {

            // Replace with Configuration() if you don't use annotations or JDK 5.0
            configuration = new Configuration();

            // This custom entity resolver supports entity placeholders in XML mapping files
            // and tries to resolve them on the classpath as a resource
            //configuration.setEntityResolver(new ImportFromClassPathEntityResolver());

            // Read not only hibernate.properties, but also hibernate.cfg.xml
            configuration.configure();

            // Assign a global, user-defined interceptor with no-arg constructor
            String interceptorName = configuration.getProperty(INTERCEPTOR_CLASS);
            if (interceptorName != null) {
                Class interceptorClass =
                        HibernateUtil.class.getClassLoader().loadClass(interceptorName);
                Interceptor interceptor = (Interceptor)interceptorClass.newInstance();
                configuration.setInterceptor(interceptor);
            }

            // Disable ThreadLocal Session/Transaction handling if CMT is used
            if (org.hibernate.transaction.CMTTransactionFactory.class.getName()
                 .equals( configuration.getProperty(Environment.TRANSACTION_STRATEGY) ) )
                useThreadLocal = false;

            if (configuration.getProperty(Environment.SESSION_FACTORY_NAME) != null) {
                // Let Hibernate bind it to JNDI
                configuration.buildSessionFactory();
            } else {
                // or use static variable handling
                sessionFactory = configuration.buildSessionFactory();
            }

        } catch (Throwable ex) {
            // We have to catch Throwable, otherwise we will miss
            // NoClassDefFoundError and other subclasses of Error
            log.error("Building SessionFactory failed.", ex);
           [b] throw new ExceptionInInitializerError(ex);[/b]
        }
    }


The SECOND time, if I refresh, and every refresh thereafter, I see the exception I complained about in my first post.

This has got to be a good lead!?

Thanks again all!!![/b]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 10, 2005 12:33 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
So obviously you are swallowing both the exception log and the exception thrown by the static initializer on startup. Find out what is causing the exception and you have your problem.

How to enable the log is explained on this page, which you should have read by now:

http://www.hibernate.org/ForumMailingli ... AskForHelp


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 10, 2005 3:16 am 
Beginner
Beginner

Joined: Thu Sep 08, 2005 9:24 pm
Posts: 20
Location: Boise Idaho
christian wrote:
So obviously you are swallowing both the exception log and the exception thrown by the static initializer on startup. Find out what is causing the exception and you have your problem.

How to enable the log is explained on this page, which you should have read by now:

http://www.hibernate.org/ForumMailingli ... AskForHelp


I followed the instructions for configuring log4j yet I see no output. I'll have to consult more documentation on that.

On a happier note. I got it working.

I had a value that was mis-entered in the hbm.xml file and it just happened to catch my eye.

I'm wondering why the exception didn't tell me more...and couldn't have just reported that?

Thanks anyhow!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 10, 2005 3:42 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What you are seeing is not the original exception. I've no idea why you don't see it though.


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