TonyW
TonyW

Reputation: 18875

resources/hibernate.cfg.xml not found

I am learning to use Hibernate in my Java web app. I have the following sessionFactory started with a configuration for the location of my hibernate.cfg.xml file, but it still complains: resources/hibernate.cfg.xml not found

The "resources" folder is a subdirectory of my "src" folder in the java project.

public class HibernateUtil
{
    private static final SessionFactory sessionFactory = new Configuration().configure("resources/hibernate.cfg.xml").buildSessionFactory();

    public static void main(String[] args)
    {
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Book book1 = new Book("John Wright", "Unknown Title");
        session.save(book1);
        tx.commit();

        System.out.println("Book committed: book title: " + book1.getTitle() + ", author is: " + book1.getAuthor());
        sessionFactory.close();
    }


}

The hibernate dependency I'm using in my pom.xml is as follows, but intelliJ warns that the buildSessionFactory() method is deprecated by highlight a "Strikethrough"

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

I am posting the error message from my i at the very bottom as well. What is going wrong? thanks

10:43:12.737 [main] INFO  o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
10:43:12.746 [main] INFO  org.hibernate.Version - HHH000412: Hibernate Core {4.3.1.Final}
10:43:12.747 [main] INFO  org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
10:43:12.749 [main] INFO  org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Caused by: org.hibernate.HibernateException: resources/hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2093)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2074)
    at persistence.HibernateUtil.<clinit>(HibernateUtil.java:14)
    ... 3 more
10:43:12.763 [main] INFO  org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: resources/hibernate.cfg.xml
10:43:12.763 [main] INFO  org.hibernate.cfg.Configuration - HHH000040: Configuration resource: resources/hibernate.cfg.xml

Process finished with exit code 1

Upvotes: 0

Views: 10743

Answers (2)

TonyW
TonyW

Reputation: 18875

thank you all for the replies.

Finally, I've figured it out. Below is my configuration in my HibernateUtil class. Because I'm using Hibernate 4.3.5, I've replaced the deprecated methods with the latest ones. Also I moved my hibernate.cfg.xml to src/main/java/resources.

static {
        try {
            Configuration configuration = new Configuration().configure("/resources/hibernate.cfg.xml");
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (HibernateException ex) {
            System.err.println("Initial sessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

Upvotes: 1

bradleyfitz
bradleyfitz

Reputation: 691

Your hibernate config file cannot be found because it is not in the root of the classpath when the webapp is started...

When you are using Maven, putting the hibernate.cfg.xml file in the src/main/resources folder should cause it to automatically get copied to WEB-INF/classes in the WAR file. Open your WAR file and see where the hibernate.cfg.xml is being placed.

If the hibernate.cfg.xml file is NOT in your WAR file ... double check the maven-war-plugin settings in your POM.

Regarding the deprecation warning, as per the 4.3 documentation, buildSessionFactory() is deprecated and you should be using buildSessionFactory(ServiceRegistry) instead.

Upvotes: 1

Related Questions