Introduction
Before we can leverage the power of Hibernate, we need to configure it properly. This includes adding the necessary dependencies, creating a configuration file or using annotations, and defining the database connection properties. This post will guide you through everything you need to get Hibernate up and running in your Java application.
1. Installing Hibernate: Maven and Gradle
Maven
Add the following dependencies to your pom.xml
:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.224</version>
</dependency>
</dependencies>
Gradle
Add the following dependencies to your build.gradle
:
dependencies {
implementation 'org.hibernate:hibernate-core:6.4.4.Final'
implementation 'org.slf4j:slf4j-simple:2.0.12'
implementation 'com.h2database:h2:2.2.224'
}
2. Hibernate Configuration File
Hibernate can be configured using either an XML configuration file (hibernate.cfg.xml
) or programmatically via annotations and Java classes.
hibernate.cfg.xml (XML-based configuration)
This file typically resides in the src/main/resources
directory:
<?xml version="1.0" encoding="utf-8"?>
<!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="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:testdb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</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">update</property>
<!-- List of annotated entity classes -->
<mapping class="com.example.model.User"/>
</session-factory>
</hibernate-configuration>
Annotation-Based Configuration (Java code)
You can also configure Hibernate using Java code without an XML file:
Configuration configuration = new Configuration();
configuration.configure(); // Looks for hibernate.cfg.xml
configuration.addAnnotatedClass(User.class);
SessionFactory sessionFactory = configuration.buildSessionFactory();
3. Understanding Hibernate Properties
- connection.driver_class: The JDBC driver class for your database (e.g.,
org.h2.Driver
). - connection.url: The database URL (e.g.,
jdbc:mysql://localhost:3306/mydb
). - connection.username/password: Credentials used to connect to the database.
- dialect: Hibernate SQL dialect for your DBMS (e.g.,
org.hibernate.dialect.MySQLDialect
). - hbm2ddl.auto: Schema management strategy (
validate
,update
,create
,create-drop
). - show_sql: If true, logs SQL statements to the console.
4. Creating a Sample Entity Class
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username")
private String username;
// Getters and setters
}
5. Building the SessionFactory
The SessionFactory
is the central factory for creating Hibernate Session
objects:
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Conclusion
With your dependencies set up and configuration in place, you now have a working Hibernate foundation. In the next part of this series, we’ll look into defining entities and mapping relationships between them to model complex data structures.
Up next: Data Modeling with Hibernate.
0 Comments