Step 2 – Configuring Hibernate

Configuring Hibernate is the process of setting up the framework to connect to a database, define mapping files and specify the persistent objects that will be used in the application.

There are following steps to perform-
1- Importing Hibernate libraries (Classes/Interfaces)
2- Creating Hibernate Configuration File

Step 1-

  • Add Hibernate libraries to your project
  • Libraries can be found on the Hibernate website
  • Libraries needed: core Hibernate framework, Hibernate annotations, and database driver

Here is an example of how to add the necessary libraries to a project using Maven:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.22.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.24</version>
</dependency>

Note – There are several alternative ways to import dependencies without using a pom.xml file:

  1. You can manually download the JAR files for the dependencies and add them to the classpath of your project.
  2. You can use the Gradle or Apache Ivy build tools, which do not require a pom.xml file. Instead, you can specify dependencies in a build.gradle or ivy.xml file.
  3. You can use a package manager such as Maven Wrapper, SDKMAN or Jabba that can download the dependencies and manage them for you, without the need for a pom.xml file.
  4. You can use a build tool like Apache Ant which uses build.xml file instead of pom.xml to manage dependencies.
  5. Some IDEs like Eclipse and IntelliJ IDEA also have support for adding dependencies to a project without using a pom.xml file.

Important-

Hibernate Interfaces/ClassDescription
Configuration classUsed to configure Hibernate settings and create a SessionFactory
Session InterfaceRepresents a single unit of work with the database, used for creating, reading, updating, and deleting objects, and committing or rolling back a transaction
SessionFactory InterfaceFactory for creating Session objects, thread-safe and can be used to create multiple sessions
Transaction InterfaceRepresents a unit of work with the database, used to group multiple operations together and commit or rollback all of them at once
Query InterfaceUsed to execute HQL (Hibernate Query Language) and native SQL queries, can be used to retrieve, update, or delete data
Criteria InterfaceUsed to create type-safe and type-sensitive queries, can be used to retrieve data based on certain criteria, retrieve specific fields, or retrieve data based on certain conditions

Step 2- Next,

  • Create a configuration file named “hibernate.cfg.xml”
  • Include connection details for the database
  • Information to include: database URL, username, password, SQL dialect

Here is an example of a basic hibernate.cfg.xml configuration file:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
        <property name="connection.username">myusername</property>
        <property name="connection.password">mypassword</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Mapping files -->
        <mapping resource="com/mypackage/mypersistentobject.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Note – There are several alternative ways to create a Hibernate configuration file, instead of using a hibernate.cfg.xml file:

1- Using a hibernate.properties file: This file can be used to store Hibernate configuration properties, such as the database connection details and other settings.

# hibernate.properties file
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/mydatabase
hibernate.connection.username=myusername
hibernate.connection.password=mypassword
hibernate.connection.pool_size=1
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.current_session_context_class=thread
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
hibernate.mapping.resource=com/mypackage/mypersistentobject.hbm.xml

2- Using a Java class: A Java class can be used to create a Hibernate configuration object, which can be used to configure Hibernate.

import org.hibernate.cfg.Configuration;

public class HibernateConfig {

    public static Configuration getConfig() {
        Configuration config = new Configuration();

        config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydatabase");
        config.setProperty("hibernate.connection.username", "myusername");
        config.setProperty("hibernate.connection.password", "mypassword");
        config.setProperty("hibernate.connection.pool_size", "1");
        config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        config.setProperty("hibernate.current_session_context_class", "thread");
        config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
        config.setProperty("hibernate.show_sql", "true");
        config.setProperty("hibernate.hbm2ddl.auto", "create");
        config.addResource("com/mypackage/mypersistentobject.hbm.xml");

        return config;
    }
}

3- Using Java-based configuration: A Java-based configuration can be used to configure Hibernate, which eliminates the need for a separate configuration file.

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateConfig {

    public static SessionFactory getSessionFactory() {

        Configuration config = new Configuration();

        // Hibernate settings equivalent to hibernnate.cfg.xml's properties
config.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
config.setProperty(Environment.URL, "jdbc:mysql://localhost:3306/mydatabase");
config.setProperty(Environment.USER, "myusername");
config.setProperty(Environment.PASS, "mypassword");
config.setProperty(Environment.POOL_SIZE, "1");
config.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL5InnoDBDialect");
config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
config.setProperty(Environment.CACHE_PROVIDER_CLASS, "org.hibernate.cache.NoCacheProvider");
config.setProperty(Environment.SHOW_SQL, "true");
config.setProperty(Environment.HBM2DDL_AUTO, "create");
config.addResource("com/mypackage/mypersistentobject.hbm.xml");

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(config.getProperties()).build();

    return config.buildSessionFactory(serviceRegistry);
}


4- Using Spring: Hibernate can be integrated with Spring, and the configuration can be done using Spring’s configuration file.

<bean id="sessionFactory"
 class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.mypackage" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>

<bean id="dataSource"
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydatabase" />
<property name="username" value="myusername" />
<property name="password" value="mypassword" />
</bean>
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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