Hi
i try to make sample web application using JSP and servlet which take input and insert in into DB using hibernate ,
but i face this Problem
Can any one help mee, how to solve this problem
Code:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError
DataAccess.DataAcess.Registration(DataAcess.java:31)
Registration.doPost(Registration.java:96)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
org.hibernate.cfg.Configuration.configure(Configuration.java:1441)
DataAccess.DAO.configure(DAO.java:26)
DataAccess.DAO.<clinit>(DAO.java:77)
DataAccess.DataAcess.Registration(DataAcess.java:31)
Registration.doPost(Registration.java:96)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
this is conguration file
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.connection.password">app</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping resource="Hibernate/Account.hbm.xml"/>
<mapping resource="Hibernate/Customers.hbm.xml"/>
</session-factory>
</hibernate-configuration>
mapping files
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 13, 2010 1:40:24 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Beans.Customers" schema="APP" table="CUSTOMERS">
<id name="custId" type="int">
<column name="CUST_ID"/>
<generator class="identity"/>
</id>
<property name="firstname" type="string">
<column length="20" name="FIRSTNAME" not-null="true"/>
</property>
<property name="lastname" type="string">
<column length="20" name="LASTNAME" not-null="true"/>
</property>
<property name="address" type="string">
<column length="20" name="ADDRESS" not-null="true"/>
</property>
<property name="username" type="string">
<column length="20" name="USERNAME" not-null="true"/>
</property>
<property name="balance" type="java.lang.Integer">
<column name="BALANCE"/>
</property>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 13, 2010 1:40:24 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Beans.Account" schema="APP" table="ACCOUNT">
<id name="id" type="int">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="username" type="string">
<column length="20" name="USERNAME" not-null="true" unique="true"/>
</property>
<property name="password" type="string">
<column length="20" name="PASSWORD" not-null="true"/>
</property>
</class>
</hibernate-mapping>
this DAO class
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package DataAccess;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public abstract class DAO {
protected DAO() {
}
private static SessionFactory configure()
{
Configuration conf = new Configuration();
URL u = ClassLoader.getSystemResource("Hibernate/hibernate.cfg.xml");
System.out.println(""+ u);
return (conf.configure(u).buildSessionFactory());
}
public static Session getSession() {
Session session = (Session) DAO.session.get();
if (session == null) {
session = sessionFactory.openSession();
DAO.session.set(session);
}
return session;
}
/////////////////////////////////////////////////
// this method to begin a transaction
/////////////////////////////////////////////////
public static void begin() {
getSession().beginTransaction();
}
////////////////////////////////////////////////
/////////////////////////////////////////////////
public static boolean commit() {
try{
getSession().getTransaction().commit();
}catch(HibernateException ex)
{
System.out.println("you have some problems");
return false;
}
return true;
}
protected static void rollback() {
try {
getSession().getTransaction().rollback();
} catch (HibernateException e) {
log.log(Level.WARNING, "Cannot rollback", e);
}
try {
getSession().close();
} catch (HibernateException e) {
log.log(Level.WARNING, "Cannot close", e);
}
DAO.session.set(null);
}
public static void close() {
getSession().close();
DAO.session.set(null);
}
private static final Logger log = Logger.getAnonymousLogger();
private static final ThreadLocal session = new ThreadLocal();
private static final SessionFactory sessionFactory = configure();
}