1. Introduction
In this post, we’ll walk through the process of setting up a basic Hibernate project using Maven. Hibernate is a powerful ORM (Object-Relational Mapping) tool for Java, and it’s often used to simplify database interactions in enterprise applications.
2. Prerequisites
- Java JDK 17 or later
- Maven installed
- A code editor (e.g., IntelliJ IDEA, Eclipse)
- A basic understanding of Java classes and interfaces
3. Step 1: Create a Maven Project
If you’re using a terminal:
mvn archetype:generate -DgroupId=org.kodejava.hibernate \
-DartifactId=hibernate-setup \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
Navigate to the project folder:
cd hibernate-setup
4. Step 2: Add Hibernate Dependencies
Edit your pom.xml
to include the Hibernate Core dependency and H2 Database (for testing):
<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>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
5. Step 3: Create Hibernate Configuration
Create a file named hibernate.cfg.xml
under src/main/resources
:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection properties -->
<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 properties -->
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- Manage schema updates automatically -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Specify annotated entity classes -->
<mapping class="org.kodejava.hibernate.Student" />
</session-factory>
</hibernate-configuration>
6. Step 4: Define a Simple Entity Class
package org.kodejava.hibernate;
import jakarta.persistence.*;
@Entity
@Table(name = "students")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public Student() {}
public Student(String name) { this.name = name; }
// getters and setters
}
7. Step 5: Write a Main Class to Test Hibernate
package org.kodejava.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateMain {
public static void main(String[] args) {
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
Session session = factory.openSession();
try {
Student student = new Student("Alice");
session.beginTransaction();
session.persist(student);
session.getTransaction().commit();
System.out.println("Student saved successfully!");
} finally {
session.close();
factory.close();
}
}
}
8. Run the Program
Compile and run the application:
mvn compile exec:java -Dexec.mainClass="org.kodejava.hibernate.HibernateMain"
9. Summary
You’ve now set up a basic Hibernate project using Maven and saved a simple entity to an in-memory database. In future posts, we’ll expand on this by adding relationships, custom queries, and performance tuning techniques.