0

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.

enter image description here

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)
vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59

3 Answers3

2

Move the file into src/main/resources where it will be copied into your classpath at build time

Reimeus
  • 158,255
  • 15
  • 216
  • 276
2

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.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
1

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.

K139
  • 3,654
  • 13
  • 17
  • It's clear about moving into resources, but how do I add another folder to classpath? – vcmkrtchyan May 28 '15 at 14:15
  • @Carmine Seems you are using Maven for build. Refer this post to add a directory to Maven classpath. http://stackoverflow.com/questions/1324767/maven-add-directory-to-classpath-while-executing-tests – K139 May 28 '15 at 14:17