简体   繁体   中英

Hibernate specified cfg.xml file [/src/main/resources/hibernate.cfg.xml] does not exist

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM