MyEclipse Forums
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
frocco
Post subject: Help with hibernate error  PostPosted: Apr 20, 2005 - 02:10 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

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
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Post subject:   PostPosted: Apr 20, 2005 - 03:41 PM
Registered Member
Registered Member


Joined: Jan 06, 2004
Posts: 23824

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

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 03:43 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

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() {
}

}
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Post subject:   PostPosted: Apr 20, 2005 - 03:47 PM
Registered Member
Registered Member


Joined: Jan 06, 2004
Posts: 23824

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?

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 04:07 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

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

What can I do?
 
 View user's profile Send private message  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 04:50 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

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" >

<!-- 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>
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Post subject:   PostPosted: Apr 20, 2005 - 04:56 PM
Registered Member
Registered Member


Joined: Jan 06, 2004
Posts: 23824

Quote:

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.

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 06:23 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

Thanks,

Same error:

Error reading resource: /Author.hbm.xml
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Post subject:   PostPosted: Apr 20, 2005 - 06:29 PM
Registered Member
Registered Member


Joined: Jan 06, 2004
Posts: 23824

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

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 06:32 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

<?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">

<!-- 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>
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Post subject:   PostPosted: Apr 20, 2005 - 06:45 PM
Registered Member
Registered Member


Joined: Jan 06, 2004
Posts: 23824

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.

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 06:47 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

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

Thanks
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Post subject:   PostPosted: Apr 20, 2005 - 06:54 PM
Registered Member
Registered Member


Joined: Jan 06, 2004
Posts: 23824

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.

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 06:58 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

Thank you so much.

Frank
 
 View user's profile Send private message  
Reply with quote Back to top
frocco
Post subject:   PostPosted: Apr 20, 2005 - 07:54 PM
Registered Member
Registered Member


Joined: Mar 04, 2005
Posts: 21

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
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT - 6 Hours
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2004 The PNphpBB Group
Credits