Forums Register Login

where to place hibernate.cfg.xml ?

+Pie Number of slices to send: Send
Hi every body
i am new to hibernate
i am getting the folloing error
----------------------------------------
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(FirstExample.java:38)
-----------------------------------------
this is my code



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.io.*;


/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(String[] args) {
Session session = null;
System.out.println("asdfasdf");
try{
// 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();
contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
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();

}

}
}

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"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc racle:thin:@172.23.162.200:1521:arti</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="hbm2ddl.auto">update</property>
<mapping resource="contact.hbm.xml"/>
</session-factory>

</hibernate-configuration>

contact.hbm.xml file is
-----------------------------------------------
<?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="Contact" table="CONTACT">
<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>

Contact.java
-------------------------------------------


/**
* @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

yours
Mallik
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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();

Mark
+Pie Number of slices to send: Send
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

hibernate.cfg.xml
contact.hbm.xml
Contact.java
FirstExample.java
and all these files are in same folder f:\test
i am able to compile java files
but while running i am getting NullPointerException
because

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

Mallik
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 8318 times.
Similar Threads
data insert error
org.hibernate.exception.GenericJDBCException
it is giving fatal exception
/hibernate.cfg.xml not found while Running simple java program
Hibernate--->Sqlserver
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 02:24:55.