aspose file tools*
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 Spring in Action 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
Author

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(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


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

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
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

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where to place hibernate.cfg.xml ?