Hibernate 指定的 cfg.xml 文件 [/src/main/resources/hibernate.cfg.xml] 不存在
Hibernate specified cfg.xml file [/src/main/resources/hibernate.cfg.xml] does not exist
我有一个 Mave + Hibernate + Eclipse 项目。
我的 hibernate.cfg.xml 在 /src/main/resources 中(应该是)
这是我的树
My tree
HibernateUtil class 启动会话(我也尝试强制 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>
有什么想法吗?
资源标签应该如下所示:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
你应该这样查找:
cfgFile = "hibernate.cfg.xml";
如果您将它放在创建的 war
文件的 WEB-INF
中。 /src/main/
层次绝对不应该是生成文件的一部分。
我有一个 Mave + Hibernate + Eclipse 项目。 我的 hibernate.cfg.xml 在 /src/main/resources 中(应该是)
这是我的树 My tree
HibernateUtil class 启动会话(我也尝试强制 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>
有什么想法吗?
资源标签应该如下所示:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
你应该这样查找:
cfgFile = "hibernate.cfg.xml";
如果您将它放在创建的 war
文件的 WEB-INF
中。 /src/main/
层次绝对不应该是生成文件的一部分。