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)
1
  • Is the file in the classpath ? Commented May 28, 2015 at 13:58

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

2 Comments

It's clear about moving into resources, but how do I add another folder to classpath?
@Carmine Seems you are using Maven for build. Refer this post to add a directory to Maven classpath. stackoverflow.com/questions/1324767/…
0

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

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.