• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

where to place hibernate.cfg.xml ?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mallik Avula
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic