In this post, I will share with you how to configure database connection information for a Hibernate in a programmatic way, rather than using XML in hibernate.cfg.xml file. Programmatic configuration can be useful in case you need to dynamically update the connection information at runtime, e.g. changing database type or database name.

First, create a new Configuration object and set the connection information like this:

Configuration config = new Configuration();

config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/bookstoredb");
config.setProperty("hibernate.connection.username", "root");
config.setProperty("hibernate.connection.password", "password");
config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
The configuration information is as same as in the hibernate.cfg.xml which you would normally do.

And if you use annotated model classes, specify each one like this:

config.addAnnotatedClass(ModelClass1.class);
config.addAnnotatedClass(ModelClass2.class);
config.addAnnotatedClass(ModelClass3.class);
In case you use XML mapping file (.hbm.xml), specify each class like this:

config.addClass(ModelClass1.class);
config.addClass(ModelClass2.class);
config.addClass(ModelClass3.class);
Then Hibernate will find the .hbm.xml files corresponding to the class names.

Then build a SessionFactory from the Configuration object, and open a Session normally:

SessionFactory sessionFactory = config.buildSessionFactory();	
Session session = sessionFactory.openSession();

// working with the session 

session.close();
sessionFactory.close();
For your reference, following is an example program that uses programmatic configuration for Hibernate:

package net.codejava.db;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateProgrammaticConfig {

	public static void main(String[] args) {
		
		
		Configuration config = new Configuration();
		config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
		config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/bookstoredb");
		config.setProperty("hibernate.connection.username", "root");
		config.setProperty("hibernate.connection.password", "password");
		config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
		config.addAnnotatedClass(Users.class);
		
		try {
			SessionFactory sessionFactory = config.buildSessionFactory();	
		    Session session = sessionFactory.openSession();
		    Users user = session.get(Users.class, 1);
		    
		    System.out.println(user.getEmail());
		    System.out.println(user.getFullName());
		    System.out.println(user.getPassword());
		    
		    session.close();
		    sessionFactory.close();
		    
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
That’s how to use programmatic configuration for Hibernate. You can watch the video version below:



 

Other Hibernate Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#3Nam2021-06-10 08:30
I see, Gustavo Matos.
Let me find out how to configure JPA programmatically then I'll let you know.
Quote
#2Gustavo Matos2021-06-10 07:05
Good morning Nam Ha Minh, my name is Gustavo, I'm using google translator because I don't speak English, I hope you're sorry for any mistakes I'm Brazilian, I'm looking for this solution for some time, but unfortunately your toturial isn't working in my application, in your tutorial you uses the hibernate.cfg.xml file, in my application I need the same solution, but I use the persistence.xml file, hibernate 5.5, if you can help me how do I do this configuration using this way? thank you so much
Quote
#1Victor Cabral Vida2020-07-02 10:11
thank you, you have resolved my problem.
Quote