Sponsored Links
Ad by Google
Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104) at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845) ... 2 more
Latest version of hibernate has changes the way of obtaining the session factory from the configuration, The above exception will occurs while creating hibernate session factory in hibernate 4.3.x. If you are getting the above exception means you are missing something at the time of creating session factory or creating session factory via wrong way.
To fix the issue and obtaining the session factory via loading configuration file in hibernate 4.3.7 which is latest stable version of hibernate you have to import org.hibernate.boot.registry.StandardServiceRegistryBuilder
Here is a HibernateUtility class to create sessionFactory in Hibernate 4.3.7
package com.hibernate.association.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateUtility { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); SessionFactory sessionFactory = configuration .buildSessionFactory(serviceRegistry); return sessionFactory; } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Now call HibernateUtility.getSessionFactory(), It will successfully return you the instance of sessionFactory.
If you are using Hibernate 4.3.x below 4.3.7 than use below given sample.
new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry();
That's it.
Sponsored Links
0 comments:
Post a Comment