You are currently viewing Getting Started with Java Hibernate

Getting Started with Java Hibernate

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:August 22, 2024

Introduction:

Hibernate is a popular and powerful object-relational mapping (ORM) framework for Java. It simplifies database interactions by providing a high-level, object-oriented API for persisting Java objects to relational databases. In this tutorial, we’ll explore the basics of Hibernate and walk through practical examples to illustrate its usage.

Prerequisites:

Before proceeding with the examples, ensure you have the following:

  1. Java Development Kit (JDK) installed (version 8 or higher).
  2. Integrated Development Environment (IDE) such as Eclipse, IntelliJ, or NetBeans.
  3. Hibernate library (JAR files) added to your project.
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>hibernate-mysql-example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <hibernate.version>5.6.15.Final</hibernate.version> <!-- Hibernate Version -->
        <jpa.version>2.1</jpa.version> <!-- JPA Version -->
        <mysql.version>8.0.33</mysql.version> <!-- MySQL Version -->
        <validation-api.version>2.0.1.Final</validation-api.version> <!-- Bean Validation API Version -->
        <hibernate-validator.version>7.0.1.Final</hibernate-validator.version> <!-- Hibernate Validator Version -->
        <hikaricp.version>5.0.1</hikaricp.version> <!-- HikariCP Version -->
        <slf4j.version>2.0.9</slf4j.version> <!-- SLF4J Version -->
    </properties>

    <dependencies>
        <!-- Hibernate Core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- Hibernate EntityManager (JPA) -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- MySQL JDBC Driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <!-- HikariCP Connection Pooling -->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>${hikaricp.version}</version>
        </dependency>

        <!-- JPA API -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>${jpa.version}</version>
        </dependency>

        <!-- Bean Validation API -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>${validation-api.version}</version>
        </dependency>

        <!-- Hibernate Validator (for Bean Validation) -->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate-validator.version}</version>
        </dependency>

        <!-- Logging with SLF4J and Logback -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.4.11</version>
        </dependency>

        <!-- JAXB for XML handling -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.3</version>
        </dependency>

        <!-- JUnit for Testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Maven Compiler Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Example 1: Setting Up Hibernate Configuration

  1. Create Hibernate Configuration File (hibernate.cfg.xml):
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <!-- Other configuration properties -->
        <mapping class="com.example.demo.Book"/>
    </session-factory>
</hibernate-configuration>

Replace the database URL, username, and password with your own database connection details.

  1. Create Hibernate Util Class:

Example 2: Creating and Persisting Entities

  1. Create Entity Class:
  1. Persisting Entities:

Example 3: Retrieving Entities

Conclusion:

This tutorial covered the basics of setting up Hibernate, creating entities, and persisting/retrieving data from a database. Hibernate simplifies database interactions by providing a convenient and object-oriented approach to working with databases. As you delve deeper into Hibernate, you’ll explore advanced features such as relationships mapping, querying with HQL (Hibernate Query Language), and more.

Leave a Reply