How do I use SessionFactory and Session in Hibernate 6?

Hibernate ORM 6 introduces several changes to its API compared to previous versions, especially in how SessionFactory and Session are used due to compliance with Jakarta EE and its updated imports (jakarta.persistence.*).
Here’s a simple guide to using SessionFactory and Session in Hibernate 6:

1. Add Hibernate Dependencies

Make sure to include the Hibernate 6 dependencies in your project. If you’re using Maven, the dependency would look like this:

<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.4.4.Final</version>
</dependency>

2. Configure Hibernate

Use or Properties for configuration:hibernate.cfg.xml

Example: hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/your_database</property>
        <property name="hibernate.connection.username">your_username</property>
        <property name="hibernate.connection.password">your_password</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

Alternatively, use Java configuration with Properties:

Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
properties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/your_database");
properties.put("hibernate.connection.username", "your_username");
properties.put("hibernate.connection.password", "your_password");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "update");

3. Create a SessionFactory

Starting with Hibernate 6, the SessionFactory should be built using the StandardServiceRegistryBuilder and MetadataSources.

Here’s an example:

Using hibernate.cfg.xml:

package org.kodejava.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    static {
        // Build the ServiceRegistry using hibernate.cfg.xml
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("hibernate.cfg.xml") // Loads hibernate.cfg.xml by default
                .build();

        try {
            // Build SessionFactory
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(registry);
            throw new ExceptionInInitializerError("SessionFactory build failed: " + e.getMessage());
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

Using Java configuration with Properties:

package org.kodejava.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;

import java.util.Properties;

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    static {
        // Create Hibernate properties
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
        properties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/your_database");
        properties.put("hibernate.connection.username", "your_username");
        properties.put("hibernate.connection.password", "your_password");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.hbm2ddl.auto", "update");

        // Build the ServiceRegistry
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .applySettings(properties)
                .build();

        try {
            // Build SessionFactory
            sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(registry);
            throw new ExceptionInInitializerError("SessionFactory build failed: " + e.getMessage());
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }
}

4. Use SessionFactory to Get a Session

A Session represents a single unit of work with the database. In Hibernate 6, the usage involves a similar pattern to previous versions.

package org.kodejava.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class App {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

        // Obtain a session
        try (Session session = sessionFactory.openSession()) {
            // Begin transaction
            session.beginTransaction();

            // Perform operations (e.g., save entities)
            MyEntity entity = new MyEntity();
            entity.setName("Example");
            session.persist(entity);

            // Commit the transaction
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Shutdown the session factory
            HibernateUtil.shutdown();
        }
    }
}

5. Entity Example

Ensure your entity classes are annotated correctly with Jakarta Persistence annotations (jakarta.persistence.*).

package org.kodejava.hibernate;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class MyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Key Updates in Hibernate 6

  1. Jakarta Imports: Hibernate moved from javax.persistence.* to jakarta.persistence.*.
  2. Default Configuration: The APIs are adaptive, but the configuration process is largely unchanged.
  3. Session Persistence: The Session.persist(Object) method is preferred over deprecated methods like save(Object).

By following these steps, you can effectively use SessionFactory and Session in Hibernate 6 for your application.

How do I map a Java class to a database table using JPA annotations?

To map a Java class to a database table using JPA annotations, you primarily use annotations provided by jakarta.persistence. Here is a step-by-step guide:

Key annotations for mapping:

  1. @Entity
    Marks the class as an entity that is mapped to a table.
  2. @Table (optional)
    Specifies the table name in the database. If omitted, the default table name is the class name.
  3. @Id
    Marks a field as the primary key.
  4. @GeneratedValue (optional)
    Specifies how the primary key value is generated (e.g., AUTO, SEQUENCE).
  5. @Column (optional)
    Represents a column in the table, providing options to customize the name, length, nullable flag, etc.
  6. Additional mapping annotations for relationships:
    For relationships between tables (e.g., one-to-many, many-to-one, etc.), you can use @OneToMany, @ManyToOne, @OneToOne, and @ManyToMany.

Example: Mapping a simple class

Here’s an example of a Java class mapped to a database table using JPA annotations:

package com.example;

import jakarta.persistence.*;

@Entity
@Table(name = "students") // Optional; defaults to "Student"
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // Auto-generate primary key
    private Long id;

    @Column(name = "full_name", nullable = false, length = 100)
    private String name;

    @Column(name = "email", unique = true, length = 150)
    private String email;

    @Column(name = "age", nullable = false)
    private int age;

    // Default constructor (needed by JPA)
    public Student() {
    }

    // Constructor with parameters and Getters/Setters
    public Student(String name, String email, int age) {
        this.name = name;
        this.email = email;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }
}

Explanation of the example:

  1. @Entity
    Declares the class as an entity tied to a database table.
  2. @Table(name = "students")
    Specifies the table name as students. If omitted, the table name would default to the class name Student.
  3. @Id and @GeneratedValue
    Defines a primary key and specifies how values are generated. Here, GenerationType.IDENTITY lets the database auto-increment the key.
  4. @Column(name = "full_name", nullable = false, length = 100)
    Maps the name property to the full_name column in the table, defines the column as non-nullable, and limits its length to 100 characters.
  5. @Column(name = "email", unique = true, length = 150)
    The email column is set to be unique, ensuring no duplicate email addresses.

Save and persist the entity:

The class can now be used with JPA to persist records in the database. For instance:

Student student = new Student("John Doe", "[email protected]", 25);

EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-pu");
EntityManager em = emf.createEntityManager();

em.getTransaction().begin();
em.persist(student);  // Insert the record into the database
em.getTransaction().commit();

em.close();
emf.close();

Make sure to configure your file with your database connection details. persistence.xml


Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.4.4.Final</version>
    </dependency>
    <dependency>
        <groupId>jakarta.persistence</groupId>
        <artifactId>jakarta.persistence-api</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

Maven Central Maven Central

How do I perform basic CRUD operations using Hibernate 6?

Basic CRUD operations with Hibernate 6 involve creating, reading, updating, and deleting records in a database using Hibernate ORM. Hibernate simplifies these operations through its API.

Below is an explanation of each CRUD operation along with corresponding examples:

1. Create (Insert)

To save a new object in the database, you use the persist() or save() method provided by Hibernate.

SessionFactory factory = new Configuration()
                .configure("hibernate.cfg.xml")
                .addAnnotatedClass(Student.class)
                .buildSessionFactory();

try (Session session = factory.openSession()) {
    // Create a new student entity
    Student student = new Student("Jane Doe");

    // Start a transaction
    session.beginTransaction();

    // Save the entity to the database
    session.persist(student);

    // Commit the transaction
    session.getTransaction().commit();

    System.out.println("Student saved successfully with ID: " + student.getId());
} finally {
    factory.close();
}

2. Read (Retrieve)

Hibernate’s get() or find() method is used to retrieve data from the database. You can fetch an object by its primary key.

try (Session session = factory.openSession()) {
    // Start a transaction (optional if only querying)
    session.beginTransaction();

    // Retrieve a student by their primary key (ID)
    int studentId = 1;  // Example ID
    Student retrievedStudent = session.get(Student.class, studentId);

    System.out.println("Retrieved Student: " + retrievedStudent);

    // Commit the transaction
    session.getTransaction().commit();
}

3. Update

To modify an existing object in the database, you first retrieve it, make changes to its fields, and then let Hibernate update it within a transaction.

try (Session session = factory.openSession()) {
    // Start a transaction
    session.beginTransaction();

    // Retrieve the student entity we want to update
    int studentId = 1;  // Example ID
    Student studentToUpdate = session.get(Student.class, studentId);

    // Modify the entity (e.g., update the name)
    if (studentToUpdate != null) {
        studentToUpdate.setName("Updated Name");
    }

    // Hibernate automatically tracks changes and applies them during commit
    session.getTransaction().commit();

    System.out.println("Student updated successfully.");
}

4. Delete

To delete an object, retrieve it first and use the delete() method to remove it from the database.

try (Session session = factory.openSession()) {
    // Start a transaction
    session.beginTransaction();

    // Retrieve the student to delete by their primary key
    int studentId = 1;
    Student studentToDelete = session.get(Student.class, studentId);

    // Delete the entity if it exists
    if (studentToDelete != null) {
        session.delete(studentToDelete);
        System.out.println("Student deleted successfully.");
    }

    // Commit the transaction
    session.getTransaction().commit();
}

Keynotes for Hibernate 6:

  1. Ensure you have the correct Hibernate dependencies in pom.xml (for Maven) or build.gradle (for Gradle).
  2. You must configure hibernate.cfg.xml, including database connection properties and mapping annotated classes.
  3. Always manage your Hibernate Session and SessionFactory carefully, and close them when done to free resources.
  4. Hibernate 6 has slight differences in config (e.g., Jakarta imports). Ensure you’re using the correct versions of annotations and configs (jakarta.persistence instead of javax.persistence).

For the provided code, it already demonstrates correct usage of creating a SessionFactory, opening a Session, performing a transaction for persist(), and closing resources. The same principles apply for all CRUD operations—use beginTransaction(), perform the operation, and commit() the transaction.


Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.4.4.Final</version>
    </dependency>
</dependencies>

Maven Central

How do I configure hibernate.cfg.xml for a simple application?

To configure hibernate.cfg.xml for a simple Hibernate application, you need to include the essential properties for Hibernate to interact with the database and map your entities.

Below is an example of hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- Database connection settings -->
        <property name="hibernate.connection.driver_class">org.h2.Driver</property>
        <property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"></property>

        <!-- Hibernate dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- JDBC connection pool settings -->
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.timeout">300</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>

        <!-- Enable Hibernate's automatic table creation -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- Show SQL logs in the console -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <!-- Add entity mappings -->
        <mapping class="org.kodejava.hibernate.Student"/>

    </session-factory>
</hibernate-configuration>

Explanation of the Configuration

  1. Database Connection Settings:
    • hibernate.connection.driver_class: Specifies the JDBC driver class (e.g., org.h2.Driver for an H2 database, com.mysql.cj.jdbc.Driver for MySQL, etc.).
    • hibernate.connection.url: JDBC URL to connect to your database.
    • hibernate.connection.username and hibernate.connection.password: Database credentials.
  2. Dialect:
    • hibernate.dialect: Hibernate’s SQL dialect for the specific database (e.g., H2Dialect, MySQLDialect, PostgreSQLDialect, etc.).
  3. Connection Pool:
    • Configures a simple C3P0 connection pool with properties like min_size, max_size, and timeout.
  4. Schema Management:
    • hibernate.hbm2ddl.auto:
      • create: Creates the schema, destroying any existing data.
      • update: Updates the schema, keeping existing data.
      • validate: Validates the schema without making changes.
      • none: Disables automatic schema management.
  5. SQL Output:
    • hibernate.show_sql: Logs executed SQL statements to the console.
    • hibernate.format_sql: Formats SQL logs for better readability.
  6. Entity Mapping:
    • <mapping class="org.kodejava.hibernate.Student"/>: Maps the Student entity to the database.

Using this Configuration

Place the hibernate.cfg.xml file in the src/main/resources directory (or in the root of your classpath). You can then use it to build a SessionFactory in your application:

SessionFactory factory = new Configuration()
        .configure("hibernate.cfg.xml")  // Load the config file
        .addAnnotatedClass(Student.class)  // Add annotated entity classes
        .buildSessionFactory();

This will allow Hibernate to connect to the database and manage your entities as per the configuration specified.

How do I create my first Hibernate entity using annotations?

To create a basic Hibernate entity using annotations, follow these steps:

1. Add Required Dependencies

Ensure you have added the required dependencies for Hibernate, JPA (jakarta.persistence), and any database (e.g., H2 for testing) in your pom.xml. For example:

<dependencies>
    <!-- Hibernate Core -->
    <dependency>
        <groupId>org.hibernate.orm</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.4.4.Final</version>
    </dependency>
    <!-- H2 Database -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>2.2.224</version>
        <scope>runtime</scope>
    </dependency>
    <!-- JPA API -->
    <dependency>
        <groupId>jakarta.persistence</groupId>
        <artifactId>jakarta.persistence-api</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

2. Create an Entity Class

An entity class represents a table in the database and should be annotated with @Entity. For example:

package org.kodejava.hibernate;

import jakarta.persistence.*;

@Entity
@Table(name = "students")  // Maps to a table named 'students'
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)  // Auto-incremented primary key
    private Long id;

    @Column(name = "name", nullable = false)  // Maps field to a column
    private String name;

    // Default constructor
    public Student() {}

    // Constructor with arguments
    public Student(String name) {
        this.name = name;
    }

    // Getters and setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Explanation of Annotations

  1. @Entity: Marks the class as an entity that maps to a database table.
  2. @Table(name = "table_name"): Specifies the name of the database table (optional). If omitted, the table will use the class name.
  3. @Id: Marks the field as the primary key.
  4. @GeneratedValue(strategy = GenerationType.IDENTITY): Specifies auto-generation of primary key values.
  5. @Column(name = "column_name"): Maps a class field to a specific table column (optional). Omitting this will map the field name to a column with the same name.

3. Specify Entity in Hibernate Configuration

Ensure this entity is configured in your hibernate.cfg.xml file, or programmatically added when building the SessionFactory. In the XML file, include:

<mapping class="org.kodejava.hibernate.Student" />

4. Persist Data Using Hibernate

You can now use Hibernate to perform CRUD operations on this entity. For example, to save a Student:

package org.kodejava.hibernate;

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

public class HibernateApp {
    public static void main(String[] args) {
        // Create a SessionFactory and configure Hibernate
        SessionFactory factory = new Configuration()
                .configure("hibernate.cfg.xml")  // Load the configuration file
                .addAnnotatedClass(Student.class)  // Add annotated class
                .buildSessionFactory();

        // Open session
        try (Session session = factory.openSession()) {
            // Create a new student entity
            Student student = new Student("John Doe");

            // Start a transaction
            session.beginTransaction();

            // Save the student to the database
            session.persist(student);

            // Commit the transaction
            session.getTransaction().commit();

            System.out.println("Student saved successfully with ID: " + student.getId());
        } finally {
            factory.close();  // Close the factory
        }
    }
}

Summary

By using annotations like @Entity, @Table, @Id, and @Column, you can define the structure of your database table directly within the Java entity class. Hibernate simplifies interacting with the database and reduces the amount of boilerplate code involved.