I am using Hibernate latest version 4.3.5.Final
.
My hibernate.cfg.xml
file content:
Finally able to fix the issue, the issue is with the way SessionFactory
is getting created.
For a starter, official document is the first place to go and I must say that Hibernate official documentation seems to be not update with latest version changes.
Fix is to apply the configuration settings to StandardServiceRegistryBuilder
instance.
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
Please refer my blog post here for more details.
In my case I ran the configuration routine twice. I had automatic configuration (ie config from xml in resources) in one method, then I implemented a configuration by setting properties in another method but I forgot to bypass the former. As soon as I did, the error gone.
You cal also fix the issue by creating the sesisonFactory this way.
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().applySettings(new Configuration().configure().getProperties()).build());
I had the exactly the same problem and the issue was, that my computer was not entered in the pg_hba.conf of the postgres database that allows which Clients(IP-Adresses) are allowed to speak with the database
The same problem occurred to me with the mysql service running, correct credentials, but the database name was misspelled, so basically the database did not exist.
This will be OK
Configuration cfg = new Configuration().configure();
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
Session session = sf.openSession();