Hibernate指定的cfg.xml文件[/src/main/resources/hibernate.cfg.xml]不存在

时间:2015-11-13 09:30:47

标签: java hibernate maven hibernate.cfg.xml

我有一个Mave + Hibernate + Eclipse项目。 我在/ src / main / resources中的hibernate.cfg.xml(应该是)

这是我的树 My tree

HibernateUtil类启动会话(我也尝试强制使用cfg.xml,因为你可以在注释部分看到没有成功)

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();
    }

这是例外

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

我也尝试将cfg文件移动到另一个目录,但我总是遇到同样的错误,我的pom.xml文件说/ src / main / resources是一个资源目录

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

资源标记应该是这样的:

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

你应该这样看:

cfgFile = "hibernate.cfg.xml";

如果将其放在创建的WEB-INF文件的war中。 /src/main/层次结构肯定不应该是生成文件的一部分。