I am creating a simple application to learn Hibernate. I am using NetBeans IDE and I created a class in com.hibernate package. The class is defined as:
package com.hibernate;
import com.mahesh.entity.UserDetails;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class hibr {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserID(1);
user.setUserName("Mahesh");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
I have defined UserDetails class as:
package com.mahesh.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
*
* @author Mahesh
*/
@Entity
public class UserDetails {
@Id
private int userID;
private String userName;
public void setUserID(int userID) {
this.userID = userID;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserID() {
return userID;
}
public String getUserName() {
return userName;
}
}
I have defined a hibernate.cfg.xml file which is in src folder(default package)
This is the error generated by NetBeans IDE.
Feb 27, 2012 8:51:35 AM org.hibernate.cfg.Configuration configure INFO: configuring from resource: /hibernate.cfg.xml Feb 27, 2012 8:51:35 AM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: Configuration resource: /hibernate.cfg.xml Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405) at org.hibernate.cfg.Configuration.configure(Configuration.java:1427) at org.hibernate.cfg.Configuration.configure(Configuration.java:1414) at com.hibernate.hibr.main(hibr.java:18) Java Result: 1