Hibernate framework is used for interacting with database records as Java Objects and vice-versa. It saves/updates java objects as database records, fetches database records as java objects etc. Hibernate performs database interaction using its Session objects. Session objects are retrieved with the help of a SessionFactory. This SessionFactory is related to a particular database.
Therefore, in order to initialize SessionFactory, database connection parameters such as database type, host, port, username, password etc. are required. Hibernate configuration involves supplying these parameters to the application so that it is able to create SessionFactory.
Hibernate can be configured in following three ways:
- Using XML file
- Using Properties file
- Programmatic configuration
1. Using XML file
Hibernate can be configured using an XML file which is named as hibernate.cfg.xml by default. This file has various property tags whose name attributes refer to a particular property and value refers to the value of that attribute. A typical XML configuration file looks as below.
<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”>
When configuring Hibernate using XML file as shown above, following code will be required:
Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); // build session factory SessionFactory sessionFactory = cfg.buildSessionFactory(); // get session Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); System.out.println(session.isConnected()); // close session session.close(); System.out.println(session.isConnected());
The above code creates an object of
org.hibernate.cfg.Configuration and builds a
org.hibernate.SessionFactory using its
buildSessionFactory method. Now, SessionFactory interacts with a database and requires database connection parameters. These are supplied to it from the configuration file of the form created above. Configuration object loads this file using its
configure method. Once SessionFactory is created, it can be used to create a
2. Using Properties File
This is another method of configuring Hibernate, a simple properties file which is a combination of name-value pairs. A snapshot of properties file looks as below:
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/codippa
hibernate.connection.username = root
hibernate.connection.password = root
The property names are significant, that is, the names of properties cannot be changed, only their values can be altered to match the database settings.
Code required to connect to a database using properties file will be as given below. The code explicitly loads a properties file from the class path into a
java.util.Properties object and provides this properties object to a
org.hibernate.cfg.Configuration by its
setProperties method. Method of creating SessionFactory from configuration remains the same.
Configuration cfg = new Configuration(); Properties p = new Properties(); //load properties file p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("hibernate.properties")); cfg.setProperties(p); // build session factory SessionFactory factory = cfg.buildSessionFactory(); // get session Session session = factory.openSession(); System.out.println(session.isConnected()); // close session session.close(); System.out.println(session.isConnected());
3. Programmatic configuration
This approach does not require any external XML or properties file. All the required configuration parameters are supplied from the code itself. For a detailed insight of this method, refer below post.
Let’s tweak in :
- Hibernate.cfg.xml or Hibernate.properties should be present under src folder or under src/main/resources folder.
org.Hibernate.cfg.Configurationis called without any arguments, then Hibernate searches for a file named hibernate.cfg.xml by default.
- When configuring Hibernate by using properties file or xml file, the file should be on the application classpath.
- SessionFactory is responsible for a single database. If your application interacts with many databases, then you will require several Session factories.
org.hibernate.Sessionrepresents a single unit of work and is opened and closed for each operation.
- SessionFactory is a global object and should be initialized only once in an application.
Hope you are taking away something new from this page. Keep visiting !!!