I upgraded my Idea from 2017.1
to 2018.1
and I have a Hibernate issue I can't solve... I've searched and it seems there are many problems with this but couldn't figure out what's the solution.
So, I'm using hibernate.version 5.2.2.Final
, spring.version 4.0.6.RELEASE
, my hibernate.cfg.xml
is in src\main\resources
folder and in my HibernateUtils I had something like this:
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();
}
}
And this was working until I upgraded my IDEA. Now I have some
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]
I tried everything I found with this error, to put the hibernate.cfg.xml
directly into src folder, to point the path to the xml file like "/resources/hibernate.cfg.xml"
or "/main/resources/hibernate.cfg.xml"
, or to leave it empty like this .configure()
but nothing seems to work.
I found in here: https://github.com/spring-projects/spring-framework/issues/21340
something that it doesn't works with Spring 4.3.17.RELEASE + Hibernate 5.2.17.Final
and works with Spring 5.0.6.RELEASE + Hibernate 5.1.14.Final
but if I change like this I get many other errors and I don't get it why my hibernate.5.2.2.Final + spring.4.0.6.RELEASE doesn't works anymore because of my IDEA. As I am using Maven, I tried to compile outside of Idea but facing the same error...
huh, maybe this will help somebody... I did it without an hibernate.cfg.xml, like this: 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;
}
}