Reputation: 5743
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
Upvotes: 2
Views: 6222
Reputation: 385
new Configuration().configure()
The configure() is referring to the classpath in build folder. I used to copy it from the src folder to build folder manually. After clean, the build folder will be removed. So, I have to repeat it. Is there any way to automate this process?
I have considered
new Configuration().configure(new File("a path/hibernate.cfg.xml"))
But the hbm file descriptions in hibernate.cfg.xml also involve a resource path. How can I mirror these files in build directory exactly as the source directory automatically?
Upvotes: 1
Reputation: 6248
Make sure hibernate.cfg.xml also in your classpath in order that JVM can see it.
Upvotes: 1
Reputation: 12054
try
new Configuration().configure(<your cfg file path>).buildSessionFactory();
Upvotes: 2