Reputation: 2626
I was going through a hibernate tutorial and faced this problem. Here is my code
public class HibernateTest {
public static void main(String[] args) {
System.out.println(args[0]);
User user = new User();
user.setId(1);
user.setName("John Smith");
SessionFactory sessionFactory = createSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
}
And here is the structure of my project.
But I'm getting an exception, saying
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2095)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2076)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
at HibernateTest.createSessionFactory(HibernateTest.java:25)
at HibernateTest.main(HibernateTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Upvotes: 0
Views: 2045
Reputation: 1
hibernate.cfg.xml keep parlel of your runnable code on the classpath. should be put under src/main/resources, which is put in the classes folder by build tools.[enter image description here][1] Project src/main/java com.package.Hibernate files of java hibernate.cfg.xml
Upvotes: 0
Reputation: 3669
It's clear in the error that hibernate.cfg.xml
file is not in CLASSPATH.
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found
To keep the file in CLASSPATH, either move it to the folder /src/main/resources (or) put the current location of the file in CLASSPATH.
Upvotes: 1
Reputation: 24403
hibernate.cfg.xml
has to be on the classpath. Files like that are considered a resource, and should be put under src/main/resources
, which is put in the classes
folder by build tools.
Upvotes: 2
Reputation: 159754
Move the file into src/main/resources
where it will be copied into your classpath at build time
Upvotes: 2