Sitemap
JavaGuides

Guides on Java, Spring Boot, REST APIs, Full-Stack Web development, Microservices, Cloud, Databases, and tools with hands-on tutorials and best practices.

Member-only story

Boost Spring Boot Performance with FetchType.LAZY in Hibernate

4 min readFeb 25, 2025

--

⚡ What is FetchType.LAZY in Hibernate?

📌 Example: Using FetchType.LAZY in One-to-Many Relationship

1️⃣ Define Department Entity (One-to-Many Relationship)

@Entity
@Table(name = "departments")
public class Department {

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

private String name;

@OneToMany(mappedBy = "department", fetch = FetchType.LAZY) // Lazy Loading
private List<Employee> employees;

// Constructors, Getters, and Setters
}

2️⃣ Define Employee Entity (Many-to-One Relationship)

@Entity
@Table(name = "employees")
public class Employee {

@Id
@GeneratedValue(strategy =…

--

--

JavaGuides
JavaGuides

Published in JavaGuides

Guides on Java, Spring Boot, REST APIs, Full-Stack Web development, Microservices, Cloud, Databases, and tools with hands-on tutorials and best practices.

No responses yet