why should I explicitly configure my config file even though I named it as "hibernate.cfg.xml"

I have my maven hibernate project with below files. When I try to create SessionFactory object with ServiceRegistryBuilder I got Dialect not set Exception. But I added dialect property in my cfg file.

Client Code with ServiceRegistryBuilder

Configuration configuration = new Configuration();
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(configuration.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = configuration
            .buildSessionFactory(serviceRegistry);

    Session session = sessionFactory.openSession();

Console Log

 log4j:WARN No appenders could be found for logger (org.jboss.logging).
 log4j:WARN Please initialize the log4j system properly.
 Exception in thread "main" 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:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at com.trendyapp.Tester.main(Tester.java:19)

But when I tried with deprecated method I works properly.

Client Code with Deprecated method

Configuration configuration = new Configuration();
ServiceRegistry serviceRegistry = new           ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
    Session session = sessionFactory.openSession();

Finally I configured my configuration file explicitly like below

Explicit configuration

    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
            .applySettings(configuration.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = configuration
            .buildSessionFactory(serviceRegistry);

Guys tell me why do I need to configure my configuration file Explictly.


Source: xml

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.