在 IDEA 2018.1 中无法使用 hibernate.5.2.2.Final + spring.4.0.6.RELEASE 找到 cfg.xml 资源
Could not locate cfg.xml resource with hibernate.5.2.2.Final + spring.4.0.6.RELEASE in IDEA 2018.1
我将我的 Idea 从 2017.1
升级到 2018.1
,但我遇到了一个我无法解决的 Hibernate 问题...我搜索过,似乎有很多问题,但无法解决想不出解决办法。
所以,我正在使用 hibernate.version 5.2.2.Final
、spring.version 4.0.6.RELEASE
,我的 hibernate.cfg.xml
在 src\main\resources
文件夹和我的HibernateUtils 我有这样的东西:
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the ServiceRegistry from hibernate.cfg.xml
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build();
// Create a metadata sources using the specified service registry.
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
} catch (Throwable ex) {
System.err.println("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();
}
}
这一直有效,直到我升级了我的 IDEA。现在我有一些
Caused by: java.lang.ExceptionInInitializerError
at org.mgo.HibernateUtils.buildSessionFactory(HibernateUtils.java:34)
at org.mgo.HibernateUtils.getSessionFactory(HibernateUtils.java:11)
Caused by: org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/main/resources/hibernate.cfg.xml]
我尝试了所有发现的错误,将 hibernate.cfg.xml
直接放入 src 文件夹,将路径指向 xml 文件,如 "/resources/hibernate.cfg.xml"
或 "/main/resources/hibernate.cfg.xml"
,或像这样将其留空 .configure()
但似乎没有任何效果。
我在这里找到:https://github.com/spring-projects/spring-framework/issues/21340
它不适用于 Spring 4.3.17.RELEASE + Hibernate 5.2.17.Final
而适用于 Spring 5.0.6.RELEASE + Hibernate 5.1.14.Final
但如果我这样更改,我会遇到许多其他错误,我不明白为什么我的 hibernate.5.2.2.Final + spring.4.0.6.RELEASE 由于我的想法而不再有效。当我使用 Maven 时,我尝试在 Idea 之外进行编译,但遇到了同样的错误...
呵呵,也许这会对某人有所帮助...我没有使用 hibernate.cfg.xml,就像这样:https://dzone.com/articles/hibernate-5-java-configuration-example
public class HibernateUtils {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (null == sessionFactory) {
try {
Configuration configuration = new Configuration();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put(Environment.DRIVER, "com.microsoft.sqlserver.jdbc.SQLServerDriver");
settings.put(Environment.URL, "jdbc:sqlserver://localhost;databaseName=DBA;instance=SQLEXPRESS");
settings.put(Environment.USER, "user");
settings.put(Environment.PASS, "xxx");
settings.put(Environment.DIALECT, "org.hibernate.dialect.SQLServerDialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
configuration.setProperties(settings);
configuration.addAnnotatedClass(MyClass1.class);
configuration.addAnnotatedClass(MyClass2.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
我将我的 Idea 从 2017.1
升级到 2018.1
,但我遇到了一个我无法解决的 Hibernate 问题...我搜索过,似乎有很多问题,但无法解决想不出解决办法。
所以,我正在使用 hibernate.version 5.2.2.Final
、spring.version 4.0.6.RELEASE
,我的 hibernate.cfg.xml
在 src\main\resources
文件夹和我的HibernateUtils 我有这样的东西:
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the ServiceRegistry from hibernate.cfg.xml
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build();
// Create a metadata sources using the specified service registry.
Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
} catch (Throwable ex) {
System.err.println("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();
}
}
这一直有效,直到我升级了我的 IDEA。现在我有一些
Caused by: java.lang.ExceptionInInitializerError
at org.mgo.HibernateUtils.buildSessionFactory(HibernateUtils.java:34)
at org.mgo.HibernateUtils.getSessionFactory(HibernateUtils.java:11)
Caused by: org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/main/resources/hibernate.cfg.xml]
我尝试了所有发现的错误,将 hibernate.cfg.xml
直接放入 src 文件夹,将路径指向 xml 文件,如 "/resources/hibernate.cfg.xml"
或 "/main/resources/hibernate.cfg.xml"
,或像这样将其留空 .configure()
但似乎没有任何效果。
我在这里找到:https://github.com/spring-projects/spring-framework/issues/21340
它不适用于 Spring 4.3.17.RELEASE + Hibernate 5.2.17.Final
而适用于 Spring 5.0.6.RELEASE + Hibernate 5.1.14.Final
但如果我这样更改,我会遇到许多其他错误,我不明白为什么我的 hibernate.5.2.2.Final + spring.4.0.6.RELEASE 由于我的想法而不再有效。当我使用 Maven 时,我尝试在 Idea 之外进行编译,但遇到了同样的错误...
呵呵,也许这会对某人有所帮助...我没有使用 hibernate.cfg.xml,就像这样:https://dzone.com/articles/hibernate-5-java-configuration-example
public class HibernateUtils {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (null == sessionFactory) {
try {
Configuration configuration = new Configuration();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put(Environment.DRIVER, "com.microsoft.sqlserver.jdbc.SQLServerDriver");
settings.put(Environment.URL, "jdbc:sqlserver://localhost;databaseName=DBA;instance=SQLEXPRESS");
settings.put(Environment.USER, "user");
settings.put(Environment.PASS, "xxx");
settings.put(Environment.DIALECT, "org.hibernate.dialect.SQLServerDialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
configuration.setProperties(settings);
configuration.addAnnotatedClass(MyClass1.class);
configuration.addAnnotatedClass(MyClass2.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}