1

I have a Mave + Hibernate + Eclipse project. My hibernate.cfg.xml in in /src/main/resources (as it should be)

This is my tree My tree

The HibernateUtil class start the session (I've also tried to force cfg.xml as you can see in the commented part with no success)

public class HibernateUtil {

private static final Logger logger = LogManager.getLogger(HibernateUtil.class);
    private final static String cfgFile ="/src/main/resources/hibernate.cfg.xml";
    private static File hibernateConfig = new File(cfgFile);

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure(hibernateConfig);
//          StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
//          ServiceRegistry serviceRegistry = srb.build();
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            logger.error("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

This is the exception

2015-11-13 10:17:56.565 [main] [ERROR] Initial SessionFactory creation failed.org.hibernate.internal.util.config.ConfigurationException: Specified cfg.xml file [/src/main/resources/hibernate.cfg.xml] does not exist
Exception in thread "main" java.lang.ExceptionInInitializerError
    at it.besmart.crud.HibernateUtil.buildSessionFactory(HibernateUtil.java:34)
    at it.besmart.crud.HibernateUtil.<clinit>(HibernateUtil.java:20)
    at it.besmart.parkserver.StartServer.updatePark(StartServer.java:70)
    at it.besmart.parkserver.StartServer.main(StartServer.java:33)
Caused by: org.hibernate.internal.util.config.ConfigurationException: Specified cfg.xml file [/src/main/resources/hibernate.cfg.xml] does not exist
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlFile(ConfigLoader.java:85)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:167)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:300)
    at it.besmart.crud.HibernateUtil.buildSessionFactory(HibernateUtil.java:26)
    ... 3 more

I've also tried to move cfg file to another dir, but i got always the same error, my pom.xml file says that /src/main/resources is a resource directory

<resources>
    <resource>
        <directory>/Users/mario/eshare/workspace/parkserver/src/main/resources</directory>
    </resource>
</resources>

Any idea?

1
  • Use relative addressing: cfgFile ="hibernate.cfg.xml" Commented Nov 13, 2015 at 9:34

1 Answer 1

1

The resource tag should look like this instead:

<resources>
  <resource>
    <directory>src/main/resources</directory>
  </resource>
</resources>

and you should look it up like this:

cfgFile = "hibernate.cfg.xml";

if you place it in the WEB-INF of the created war file. The /src/main/ hierarchy definitely should not be part of the generated file.

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

1 Comment

I create a jar file, not a war, anyway yours is a good point, but I'm not able to change the effective pom.xml to use the correct paths (indeed all my directory have full paths...)

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.