Welcome to the Hibernate Tutorials repository! 🚀 This repository contains comprehensive learning resources, code snippets, example projects, and documentation that I used while learning Hibernate.
Whether you are a beginner or an experienced developer, this repository will help you understand Hibernate ORM, its features, and how to integrate it into Java applications.
- Introduction to Hibernate
- Installation & Setup
- Hibernate Core Concepts
- Mapping in Hibernate
- Working with Hibernate Annotations
- Hibernate Query Language (HQL)
- Integrating Hibernate with Spring Boot
- Performance Optimization & Caching
- Error Handling & Debugging
- Resources & References
Hibernate is an ORM (Object-Relational Mapping) framework for Java that simplifies database interactions by mapping Java objects to database tables. It eliminates the need for complex SQL queries and provides a high-level abstraction for database operations.
✔ Lightweight & Open-source
✔ Automatic ORM Mapping (No need for manual SQL queries)
✔ Supports Caching (First-Level & Second-Level Caching)
✔ Database Independence (Supports multiple databases)
✔ Annotations & XML-based Configurations
✔ Integrates with Spring & JPA
- Java 8+ installed
- Maven or Gradle for dependency management, I used Maven fir this project.
- MySQL/PostgreSQL or any relational database, I used PostgresSQL.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>7.x.x</version>
</dependency>- Session & SessionFactory
- Transaction Management
- Jakarta Persistence Context
- Configuration (XML vs Annotations)
- One-to-One Mapping
- One-to-Many Mapping
- Many-to-Many Mapping
- Cascade Operations
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
private List<Course> courses;
}- @Entity, @Table, @Id, @GeneratedValue
- @OneToOne, @OneToMany, @ManyToMany
- @Column, @Transient, @Enumerated
Example:
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_name")
private String name;
@Column(unique = true, nullable = false)
private String email;
}- Basic Queries (
SELECT, UPDATE, DELETE) - Named Queries
- Pagination & Sorting
String hql = "FROM Employee WHERE salary > :salary";
Query query = session.createQuery(hql);
query.setParameter("salary", 50000);
List<Employee> employees = query.list();- Spring Boot + Hibernate Configuration
- Using Spring Data JPA with Hibernate
- Transaction Management with @Transactional
# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/your_db
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true- First-Level & Second-Level Caching
- Batch Processing & Fetch Strategies
- Lazy vs Eager Loading
Example of Caching Configuration:
@Cacheable
@Entity
public class Product {
@Id
private Long id;
private String name;
}- Common Hibernate Exceptions & Solutions
- Enabling Hibernate Logging
- SQL Injection Prevention
# Enable Hibernate SQL Logging
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql=TRACE- Hibernate Official Documentation
- Spring Boot + Hibernate Guide
- JPA & Hibernate Best Practices
- Hibernate GitHub Repository
- Hibernate Tutorial Video by Telusko
Want to contribute? Feel free to submit a Pull Request with new tutorials, improvements, or additional examples!
If you have any queries, feel free to reach out:
📩 Email: ganesh21renuse3@gmail.com
🌐 GitHub: GaneshRenuse
If you find this repository helpful, please star ⭐ it to show support! 🚀