-->
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: Servlet/PostgreSQL example from documentation Hibernate2.1.1
PostPosted: Tue Jan 06, 2004 5:03 pm 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
Hi,

I've checked the FAQs and I couldn't find an answer, but I keep getting:

Code:
java.lang.ExceptionInInitializerError at net.sf.hibernate.examples.quickstart.CatInsert.processRequest(CatInsert.java:59)


I am trying out the very first tutorial example in the reference documentation. It is basically copy and pasted (HibernateUtil is word for word), except for this servlet CatInsert.java. Line 59 is the HibernateUtil.currentSession() line:

Code:
package net.sf.hibernate.examples.quickstart;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.security.*;

import net.sf.hibernate.FetchMode;
import net.sf.hibernate.FlushMode;
import net.sf.hibernate.LockMode;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.cfg.Environment;
import net.sf.hibernate.expression.Example;
import net.sf.hibernate.expression.Expression;
import net.sf.hibernate.expression.MatchMode;

/**  Used as a development tool to view current session data.  Used during the debugging process.
*  This should never make it into a release version.
* @author  rpew
* @version
*/
public class CatInsert extends HttpServlet {
   
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
       
    }
   
    /** Destroys the servlet.
     */
    public void destroy() {
       
    }
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet</title>");
        out.println("</head>");
        out.println("<body>");
       
        try{
            Session session;
            session = HibernateUtil.currentSession();

            Transaction tx= session.beginTransaction();

            Cat princess = new Cat();
            String name = (String) request.getAttribute("name");
            char sex = ((Character) request.getAttribute("sex")).charValue();
            float weight = ((Float) request.getAttribute("weight")).floatValue();

            princess.setName(name);
            princess.setSex(sex);
            princess.setWeight(weight);

            session.save(princess);
            tx.commit();

            HibernateUtil.closeSession();
            out.println("Cat Added.<BR><BR>");
            out.println(name + "," + sex + "," + weight + "<BR><BR>");
        } catch (ExceptionInInitializerError e) {
            e.printStackTrace(out);
        } catch (Exception e) {
            e.printStackTrace(out);
        }
                       
        out.println("<a href='catdisplay'>LOOK AT CATS</a>");
        out.println("</body>");
        out.println("</html>");
       
        out.close();
    }
   
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }           
}

[/code]


Top
 Profile  
 
 Post subject: interestingly...
PostPosted: Tue Jan 06, 2004 5:13 pm 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
I have another servlet CatDisplay.java (included below). On the exact same line (HibernateUtil.currentSession()) I am getting at NoClassDefFoundException instead of InitializationError:
Code:
package net.sf.hibernate.examples.quickstart;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.security.*;

import net.sf.hibernate.FetchMode;
import net.sf.hibernate.FlushMode;
import net.sf.hibernate.LockMode;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Query;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.cfg.Environment;
import net.sf.hibernate.expression.Example;
import net.sf.hibernate.expression.Expression;
import net.sf.hibernate.expression.MatchMode;
/**  Used as a development tool to view current session data.  Used during the debugging process.
*  This should never make it into a release version.
* @author  rpew
* @version
*/
public class CatDisplay extends HttpServlet {
   
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
       
    }
   
    /** Destroys the servlet.
     */
    public void destroy() {
       
    }
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Select query</title>");
        out.println("</head>");
        out.println("<body>");
        try {
            Session session;
            session = HibernateUtil.currentSession();

            Transaction tx= session.beginTransaction();

            Query query = session.createQuery("select cat from Cat as cat");
            for (Iterator it = query.iterate(); it.hasNext();) {
                Cat cat = (Cat) it.next();
                out.println("Cat: " + cat.getName() + "<BR>");
            }

            tx.commit();

            HibernateUtil.closeSession();

        } catch (HibernateException e) {
                out.println("BOGUS<BR>");
        }
            out.println("<a href='catinsert.html'>ADD CATS</a>");
            out.println("</body>");
            out.println("</html>");       
            out.close();
    }
   
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }           
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 5:59 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Both of these errors point to an unchecked exception being thrown in a static initializer. Are you using the HibernateUtil class defined here? http://forum.hibernate.org/viewtopic.php?t=926746&highlight=hibernateutil

If so, it's no wonder. If Hibernate is not configured correctly the resulting HibernateException is wrapped in a RuntimeException. (BAD, BAD, BAD!) Inside of a static initializer this can cause both of the exceptions you've seen. It's really bad design, btw. Don't do it. :) A better thing to do in this case is to log the exception, so at least somewhere you can have an accurate stacktrace. Better yet, don't execute the config code (or preferably any code that has unchecked exceptions) in a static initailizer.

Did I mention that this is bad? :P


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 6:01 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
That advice was also given later on in the HibernateUtil thread linked above. Try it out...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 11:31 am 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
greg_barton wrote:
That advice was also given later on in the HibernateUtil thread linked above. Try it out...


Hi,

This helped a bit.. it turns out it can't find my Cat.hbm.xml, but I'm pretty sure it's in the right directory. I also tried it in the WEB-INF/classes directory to no avail. Please help!

Here is the structure:

WEB-INF/classes
-- hibernate.cfg.xml
net
sf
examples
quickstart
-- Cat.class
-- HibernateUtil.class
-- CatInsert.class
-- Cat.hbm.xml

where hibernate.cfg.xml is:

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
        <property name="show_sql">true</property>
        <property name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Mapping files -->
        <mapping resource="Cat.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 11:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
the resource property should be
<mapping resource="quickstart/Cat.hbm.xml"/>


Top
 Profile  
 
 Post subject: nevermind
PostPosted: Wed Jan 07, 2004 11:38 am 
Beginner
Beginner

Joined: Tue Jan 06, 2004 4:51 pm
Posts: 48
Sorry. Heh.. needed to provide the full path in the mapping resource.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.