user2889467
user2889467

Reputation:

Exception in thread "main" org.hibernate.HibernateException: hibernate.cfg.xml not found

This question seems to be repeated but i tried all possibilities i could do but not able to resolve this error. please help me i am beginner in Hibernate.

-FirstProject

--->src

  ------>myPackage

       --> Employee.java
       --> StoreData.java

--->Employee.hbl.xml

--->hibernate.cfg.xml

Both xml files are under src folder and here my code goes

package mypackage;

**public class Employee** 
{

    private int id;
    private String FirstName,LastName;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id=id;
    }

    public String getFirstName()
    {
        return FirstName;
    }

    public void setFirstName(String FirstName)
    {
        this.FirstName=FirstName;
    }

    public String getLastName()
    {
        return LastName;
    }

    public void setLastName(String LastName)
    {
        this.LastName=LastName;
    }
}

Employee.hbl.xml

<class name="mypackage.Employee" table="emp1000">  

   <id name="id">  
      <generator class="assigned"></generator>  
   </id>       
   <property name="firstName" column="fname" type="String"></property>  
   <property name="lastName" column="lname" type="String"></property>   

 </class>

hibernate.cfg.xml

<session-factory>  
    <property name="hbm2ddl.auto">update</property>  
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
    <property name="connection.username">system</property>  
    <property name="connection.password">12345</property>  
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
<mapping resource="First/src/Employee.hbm.xml"/>  
</session-factory>  

StoreData.java

package mypackage;

public class StoreData { 

    public static void main(String[] args) {  

        SessionFactory sF = new Configuration().
                configure("/First/src/mypackage/hibernate.cfg.xml").buildSessionFactory();

        Session session=sF.openSession();  
        Transaction t=session.beginTransaction();  
        Employee e1=new Employee();  
        e1.setId(115);  
        e1.setFirstName("Madu");  
        e1.setLastName("biradar");  
        session.persist(e1);
        t.commit();  
        session.close();  
        System.out.println("successfully saved");  
        }  
    }  

Thanks to all who would give any sort of suggestion...

Upvotes: 0

Views: 1501

Answers (2)

Ankur Singhal
Ankur Singhal

Reputation: 26067

i hope Employee.hbl.xml is a typo, you have mapped with name hbm, and file name is hbl, maybe just a type and is no related the exact issue, also post the complete stacktrace

are you able to connect using below connection properties

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>

Upvotes: 1

Koen Weyn
Koen Weyn

Reputation: 1004

Configuration.configure expects a 'class path' or 'resource' path (in your case starting from src). So if you remove '/First/src/mypackage' from your path, that should solve the file not found error.

And on the same note, you should also remove it from your reference to Employee.hbm.xml.

For further info, look at the docs for getResource in java.lang.Class (http://docs.oracle.com/javase/7/docs/api/index.html?java/lang/Class.html)

Upvotes: 1

Related Questions