2

I am very new to Hibernate ,http://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide
I followed this link to create a Hibernate application,but i am getting th e following error: 11:31:31,707 INFO [main] Environment:584 - using JDK 1.4 java.sql.Timestamp handling 11:31:31,738 INFO [main] Configuration:1423 - configuring from resource: hibernate.cfg.xml 11:31:31,738 INFO [main] Configuration:1400 - Configuration resource: hibernate.cfg.xml

11:31:33,501 INFO [main] Configuration:553 - Reading mappings from resource : employee.hbm.xml Exception in thread "main" org.hibernate.MappingNotFoundException: resource: employee.hbm.xml not found at org.hibernate.cfg.Configuration.addResource(Configuration.java:563) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505) at org.hibernate.cfg.Configuration.configure(Configuration.java:1425) at pack.storedata.main(storedata.java:13)

hibernate.cfg.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration>
   <session-factory>

   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost:3306/test
   </property>
   <property name="hibernate.connection.username">
    root
   </property>
   <property name="hibernate.connection.password">
      root
   </property>
   <!-- List of XML mapping files -->
   <mapping resource="employee.hbm.xml" />
</session-factory>
</hibernate-configuration> 

employee.hbm.xml:

<?xml version='1.0'?>  
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-mapping>
  <class name="pack.employee" table="emp1000">  
    <id name="id">  
     <generator class="assigned"></generator>  
    </id>  

    <property name="firstName"></property>  
    <property name="lastName"></property>  

  </class>  

 </hibernate-mapping>

storedata.java:

package pack;

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

public class storedata {  
public static void main(String[] args) {  

    //creating configuration object  
    Configuration cfg=new Configuration();  
    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  

    //creating seession factory object  
    SessionFactory factory=cfg.buildSessionFactory();  

    //creating session object  
    Session session=factory.openSession();  

    //creating transaction object  
    Transaction t=session.beginTransaction();  

    employee e1=new employee();  
    e1.setId(115);  
    e1.setFirstName("sonoo");  
    e1.setLastName("jaiswal");  

    session.persist(e1);//persisting the object  

    t.commit();//transaction is committed  
    session.close();  

    System.out.println("successfully saved");  

}  
} 

employee.java:

package pack;


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;  
}  


}  

Project Structure:

Hibernate
 ->src
   ->pack
     ->employee.java
     ->storedata.java
   -> employee.hbm.xml
   -> hibernate.cfg.xml
5
  • This is probably just a file path issue. Commented Jul 7, 2015 at 7:08
  • your hibernate.cfg is not able to find employee.hbm. may b path problem. where you have kept cfg and hbms? Commented Jul 7, 2015 at 7:11
  • I have edited my question,I have added my project structure .I have kept the cfg and hbms file under src folder Commented Jul 7, 2015 at 7:15
  • Try giving a full path for employee.hbm.xml and see if that's really your problem. Commented Jul 7, 2015 at 7:24
  • Yes ,I added the full path even then I am getting the same error .Edited path like : C:\java_workspace\firsthb\src\ employee.hbm.xml Commented Jul 7, 2015 at 7:27

4 Answers 4

2

I think you need to put the hibernate.cfg.xml and employee.hbm.xml files inside resource folder.

<mapping resource="employee.hbm.xml/>

Else place hibernate.cfg.xml inside resource folder and place employee.hbm.xml with employee.java (model) in a separate package, then you need to give correct path name while mapping:

<mapping resource="pack.employee.hbm.xml"/>
Sign up to request clarification or add additional context in comments.

Comments

1

just do the following changes in hibernate.cfg.xml-

<mapping resource="employee.hbm.xml"/>

and in StoreData.java file

cfg.configure("hibernate.cfg.xml");

Comments

0

The error message is very clear.

MappingNotFoundException: resource: employee.hbm.xml not found at 

I think you have missed mapping of your xml file please try to check the required xml file, probably you have misspelled it.

example is working fine by the way in my machine

1 Comment

I have mapped my xml files properly and I have spelled the mapping file correctly .even then I am getting the same error.
0

You are missing the opening tag

<hibernate-mapping> 

in your employee.hbm.xml file.

2 Comments

Thanks Vaibhav Gupta,But even then I am getting the same error
Can you please update your employee.hbm.xml adding <hibernate-mapping>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.