my dog learned polymorphism*
The moose likes Other Application Frameworks and the fly likes /hibernate.cfg.xml not found while Running simple java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "/hibernate.cfg.xml not found while Running simple java program" Watch "/hibernate.cfg.xml not found while Running simple java program" New topic
Author

/hibernate.cfg.xml not found while Running simple java program

kalai arasan
Greenhorn

Joined: Aug 18, 2009
Posts: 24
Hi All,

/hibernate.cfg.xml not found
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NullPointerException
at com.qwest.appointment.FirstExample.main(FirstExample.java:37)



While Running simple java program with Hibernate in eclipse, i am getting this exception..

I have written separately hibernate.cfg.xml and Employee.hbm.xml under WEB-INF folder


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

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/HibernateExample</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Employee.hbm.xml
=============

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.qwest.appointment.Employee" table="Employee">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>


FirstExample.java
============

package com.qwest.appointment;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
* @author Kalai
*
*
* Hibernate example to insert data into employee table
*/
public class FirstExample {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new

Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Employee employee = new Employee();
employee.setId(3);
employee.setFirstName("Deepak");
employee.setLastName("Kumar");
employee.setEmail("deepak_38@yahoo.com");
session.save(employee);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}

Employee.java
=========

package com.qwest.appointment;

/**
* @author Kalai
*
* Java Class to map to the datbase Contact Table
*/
public class Employee {
private String firstName;
private String lastName;
private String email;
private long id;

/**
* @return Email
*/
public String getEmail() {
return email;
}

/**
* @return First Name
*/
public String getFirstName() {
return firstName;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}

/**
* @param string Sets the Email
*/
public void setEmail(String string) {
email = string;
}

/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}

/**
* @return ID Returns ID
*/
public long getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(long l) {
id = l;
}

}


Have any one encountered this in your experience..


Thanks,
Kalai.


Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 198

For your app to find the hibernate.cfg.xml, it has to be at the root of your classpath.
 
 
subject: /hibernate.cfg.xml not found while Running simple java program
 
Similar Threads
data insert error
org.hibernate.exception.GenericJDBCException
Hibernate exception problem
StaleStateException on session.flush()
Odd NullPointerException