This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes where to place hibernate.cfg.xml ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "where to place hibernate.cfg.xml ?" Watch "where to place hibernate.cfg.xml ?" New topic

where to place hibernate.cfg.xml ?

Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
Hi every body
i am new to hibernate
i am getting the folloing error
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(
this is my code

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

* @author Deepak Kumar
* Hibernate example to inset data into Contact table
public class FirstExample {
public static void main(String[] args) {
Session session = null;
// This step will read hibernate.cfg.xml and prepare hibernate for use
System.out.println("Inserting Record");
Configuration cfg = Configuration().configure(new File("f:/test/hibernate.cfg.xml"));
SessionFactory sessionFactory = cfg.buildSessionFactory();
System.out.println("Inserting Record");
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
}catch(Exception e){
// Actual contact insertion will happen at this step



this is just for testing an example

hibernate.cfg.xml file is
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc racle:thin:@</property>
<property name="connection.username">aims</property>
<property name="connection.password">aims</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="">update</property>
<mapping resource="contact.hbm.xml"/>


contact.hbm.xml file is
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<class name="Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>

<property name="firstName">
<column name="FIRSTNAME" />
<property name="lastName">
<column name="LASTNAME"/>
<property name="email">
<column name="EMAIL"/>

* @author Deepak Kumar
* Java Class to map to the datbase Contact Table
public class Contact {
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;


i hope FirstExample file is unable to find two xml files
so let me know where to place those file or how to solve this

thanks in advance


Thanks & Regards<br />Mallik Avula<br />SCJP1.4
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Hibernate.cfg.xml needs to be where you specified it to be.

The individual mapping files needs to be on th eclasspath - typically in the same place as the class file of the POJO they map.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

The easiest way, is to have it in the root of your classpath.

So your

Configuration cfg = Configuration().configure(new File("f:/test/hibernate.cfg.xml"));

line does not have to have a drive letter hard-coded in your code. What if you give your stuff to someone else who doesn't have an F: drive.

so the line would be

Configuration cfg = Configuration().configure();


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Mallik Avula
Ranch Hand

Joined: Nov 30, 2006
Posts: 86
hi thanks for your great interest

i hope i have to clear a bit more
look at this you will get some better idea

i created

and all these files are in same folder f:\test
i am able to compile java files
but while running i am getting NullPointerException

Configuration cfg = Configuration().configure(new File("f:/test/hibernate.cfg.xml"));
SessionFactory sessionFactory = cfg.buildSessionFactory();

is not working
i hope the problem will be java file unable to locate xml files

how to do this (ie classpath setting )
give some idea

any help greatly cherished

I agree. Here's the link:
subject: where to place hibernate.cfg.xml ?
It's not a secret anymore!