Hibernate Programmatic Configuration Example
- Details
- Written by Nam Ha Minh
- Last Updated on 26 September 2019   |   Print Email
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:
- Hibernate Hello World Tutorial for Beginners with Eclipse and MySQL
- Java Hibernate JPA Annotations Tutorial for Beginners
- Hibernate One-to-One Association on Primary Key Annotations Example
- Hibernate One-to-Many Association Annotations Example
- Hibernate Many-to-Many Association Annotations Example
- Hibernate Enum Type Mapping Example
- Hibernate Binary Data and BLOB Mapping Example
- Hibernate Query Language (HQL) Example
- Java Hibernate Reverse Engineering Tutorial with Eclipse and MySQL
Comments
Let me find out how to configure JPA programmatically then I'll let you know.