-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate with web application problem
PostPosted: Thu May 13, 2010 8:40 am 
Newbie

Joined: Thu May 13, 2010 8:31 am
Posts: 2
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();
}


Top
 Profile  
 
 Post subject: Re: Hibernate with web application problem
PostPosted: Fri May 14, 2010 3:59 am 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
It seems the hibernate.cfg.xml isn't accessible from your classloader.
What you get at the code below?
Code:
URL u = ClassLoader.getSystemResource("Hibernate/hibernate.cfg.xml");
System.out.println(""+ u);

if 'URL u' is null try calling conf.configure() without any argument. (hibernate looks for hibernate.cfg.xml in the current classloader)

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: Hibernate with web application problem
PostPosted: Fri May 14, 2010 5:41 am 
Newbie

Joined: Thu May 13, 2010 8:31 am
Posts: 2
hallmit wrote:
It seems the hibernate.cfg.xml isn't accessible from your classloader.
What you get at the code below?
Code:
URL u = ClassLoader.getSystemResource("Hibernate/hibernate.cfg.xml");
System.out.println(""+ u);

if 'URL u' is null try calling conf.configure() without any argument. (hibernate looks for hibernate.cfg.xml in the current classloader)



Ok , but when i print the URL u , it give me the link absolute path of the configuration file in webINF


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