facebook

Help with hibernate error

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #228371 Reply

    Frank
    Member

    Hello,

    I am a newbie with hibernate, and have generated a default app in ME.

    When I run it in Tomcat 5, I get:

    exception

    org.apache.jasper.JasperException
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    java.lang.NullPointerException
    org.apache.jsp.index_jsp._jspService(index_jsp.java:64)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    It is dying on: hibernateSession = HibernateSessionFactory.currentSession();

    Here is my code.

    <%@ page language=”java” import=”java.util.*,net.sf.hibernate.*,com.stemc.*” %>
    <%@ page import=”net.sf.hibernate.Session” %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
    Session hibernateSession = null;
    Transaction myTransaction = null;
    Iterator authors = null;

    try {
    hibernateSession = HibernateSessionFactory.currentSession();
    // myTransaction = hibernateSession.beginTransaction();
    // Criteria query = hibernateSession.createCriteria(Author.class);
    // authors = query.list().iterator();
    // myTransaction.commit();
    // hibernateSession.close();
    } catch (Exception e) {
    out.println(e.getMessage().toString());
    } finally {
    try { hibernateSession.close();}
    catch (Exception e2) {;}
    }
    %>

    Thanks

    Frank

    #228385 Reply

    Riyad Kalla
    Member

    Frank, what is currentSession() doing that would cause an NPE? Please paste the code of that method.

    #228388 Reply

    Frank
    Member

    This was generated by ME

    package com.stemc;
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.cfg.Configuration;

    /**
    * Configures and provides access to Hibernate sessions, tied to the
    * current thread of execution. Follows the Thread Local Session
    * pattern, see {@link http://hibernate.org/42.html}.
    */
    public class HibernateSessionFactory {

    /**
    * Location of hibernate.cfg.xml file.
    * NOTICE: Location should be on the classpath as Hibernate uses
    * #resourceAsStream style lookup for its configuration file. That
    * is place the config file in a Java package – the default location
    * is the default Java package.<br><br>
    * Examples: <br>
    * <code>CONFIG_FILE_LOCATION = “/hibernate.conf.xml”.
    * CONFIG_FILE_LOCATION = “/com/foo/bar/myhiberstuff.conf.xml”.</code>
    */
    private static String CONFIG_FILE_LOCATION = “/hibernate.cfg.xml”;

    /** Holds a single instance of Session */
    private static final ThreadLocal threadLocal = new ThreadLocal();

    /** The single instance of hibernate configuration */
    private static final Configuration cfg = new Configuration();

    /** The single instance of hibernate SessionFactory */
    private static net.sf.hibernate.SessionFactory sessionFactory;

    /**
    * Returns the ThreadLocal Session instance. Lazy initialize
    * the <code>SessionFactory</code> if needed.
    *
    * @return Session
    * @throws HibernateException
    */
    public static Session currentSession() throws HibernateException {
    Session session = (Session) threadLocal.get();

    if (session == null) {
    if (sessionFactory == null) {
    try {
    cfg.configure(CONFIG_FILE_LOCATION);
    sessionFactory = cfg.buildSessionFactory();
    }
    catch (Exception e) {
    System.err.println(“%%%% Error Creating SessionFactory %%%%”);
    e.printStackTrace();
    }
    }
    session = sessionFactory.openSession();
    threadLocal.set(session);
    }

    return session;
    }

    /**
    * Close the single hibernate session instance.
    *
    * @throws HibernateException
    */
    public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);

    if (session != null) {
    session.close();
    }
    }

    /**
    * Default constructor.
    */
    private HibernateSessionFactory() {
    }

    }

    #228391 Reply

    Riyad Kalla
    Member

    Do me a favor, set a breakpoint on the first line of currentSession, and then step line by line through the method until the exception is thrown, which line was it?

    #228397 Reply

    Frank
    Member

    I am having trouble getting debug to work.
    Failed to connect to remote VM

    What can I do?

    #228400 Reply

    Frank
    Member

    I get: Error reading resource: /Author.hbm.xml

    Here is code:
    <?xml version=”1.0″?>
    <!DOCTYPE hibernate-mapping PUBLIC
    “-//Hibernate/Hibernate Mapping DTD 2.0//EN”
    http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd&#8221; >

    <!– DO NOT EDIT: This is a generated file that is synchronized –>
    <!– by MyEclipse Hibernate tool integration. –>
    <!– Created Tue Apr 19 14:10:14 EDT 2005 –>
    <hibernate-mapping package=””>

    <class name=”Author” table=”author”>
    <id name=”authorId” column=”author_id” type=”java.lang.String”>
    <generator class=”uuid.hex”/>
    </id>

    <property name=”firstName” column=”first_name” type=”java.lang.String” not-null=”true” />
    <property name=”lastName” column=”last_name” type=”java.lang.String” not-null=”true” />
    <property name=”homePhone” column=”home_phone” type=”java.lang.String” not-null=”true” />
    </class>

    </hibernate-mapping>

    #228401 Reply

    Riyad Kalla
    Member

    I am having trouble getting debug to work.
    Failed to connect to remote VM

    Check the debugging section here: http://www.myeclipseide.com/images/tutorials/quickstarts/webprojects/

    You don’t need to connect to anything, it is automatic if you started the app server with MyEclipse, just set a break point and run it.

    #228407 Reply

    Frank
    Member

    Thanks,

    Same error:

    Error reading resource: /Author.hbm.xml

    #228408 Reply

    Riyad Kalla
    Member

    What does your hibernate.cfg.xml file look like, where is your Author.hbm.xml file located in your project?

    #228409 Reply

    Frank
    Member

    <?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”&gt;

    <!– DO NOT EDIT: This is a generated file that is synchronized –>
    <!– by MyEclipse Hibernate tool integration. –>
    <hibernate-configuration>

    <session-factory>
    <!– properties –>
    <property name=”connection.username”>root</property>
    <property name=”connection.url”>jdbc:mysql://10.x.x.x:3306/library</property>
    <property name=”dialect”>net.sf.hibernate.dialect.MySQLDialect</property>
    <property name=”connection.password”>password</property>
    <property name=”connection.driver_class”>com.mysql.jdbc.Driver</property>

    <!– mapping files –>
    <mapping resource=”/Author.hbm.xml”/>

    </session-factory>

    </hibernate-configuration>

    #228410 Reply

    Riyad Kalla
    Member

    1) Where is your hibernate.cfg.xml file located?
    2) Where is your Author.hbm.xml file located?

    I believe this is a simple path issue as the path you have for Author implies that it is in the root of your project, which I doubt it is and it shouldn’t actually be. But I just want to make sure.

    #228411 Reply

    Frank
    Member

    C:\tomcat\webapps\authors\WEB-INF\classes

    Thanks

    #228413 Reply

    Riyad Kalla
    Member

    Then that means they are BOTH in the root of your /src tree of your project, if this is the case, edit the hibernate.cfg.xml file and remove the forward slash before Author.

    #228414 Reply

    Frank
    Member

    Thank you so much.

    Frank

    #228417 Reply

    Frank
    Member

    I also had to change my Author.hbm.xml
    from
    <class name=”Author” table=”author”>
    to
    <class name=”com.stemc.Author” table=”author”>

    Regards,

    Frank

Viewing 15 posts - 1 through 15 (of 15 total)
Reply To: Help with hibernate error

You must be logged in to post in the forum log in