Chapter 5.1☕ 14 min read

Lombok Integration

Same masala, no grinding. Let Lombok do the boilerplate for you.

01The Concept: Boilerplate Code

The Hyderabad Spice Packet Analogy:

Making a traditional Hyderabadi Biryani from scratch means buying whole spices, grinding them, and roasting them mdash; taking hours. But if you buy a ready-made spice packet from the store, you get the exact same masala instantly without the hard work.

In Java, Getters, Setters, and Constructors are the “grinding spices” process. Lombok is the spice packet. You just put an annotation like @Data on your class, and Lombok secretly injects all the getters and setters into the compiled bytecode.

02Technical Explanation
  1. @Data: A shortcut that bundles @Getter, @Setter, @ToString, and @EqualsAndHashCode.
  2. @Builder: Allows you to create objects using the Builder design pattern (e.g., Book.builder().title("X").build()).
  3. @Slf4j: Automatically injects a logger variable named log into your class so you don’t have to write LoggerFactory.getLogger(…).
03Full Working Code: The POM Setup and Entity

To use Lombok, you must add it to your pom.xml.

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
package com.devinhyderabad;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

// 1. @Data generates getters, setters, toString, equals, hashCode
// 2. @NoArgsConstructor is required by JPA/Hibernate
// 3. @AllArgsConstructor is useful for manual object creation
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Book {

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

private String title;
private String author;

// Look! No getters, no setters, no constructors written manually.
}
04Using the Logger and Builder in a Service
package com.devinhyderabad;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j // Injects the ’log’ variable automatically
public class BookService {

public void createBook() {
// Using the Lombok Builder pattern
Book book = Book.builder()
.title("Spring Boot Guide")
.author("Deva")
.build();

// Using the injected logger
log.info("Created book with title: {}", book.getTitle());
}
}
05Why It Matters / Interview Note

Interview Question: “How does Lombok actually work under the hood? Does it slow down runtime?”

Answer: Lombok works at compile-time, not runtime. It uses the Java Annotation Processing API (JSR 269) to hook into the compiler. When you compile your code, Lombok modifies the Abstract Syntax Tree (AST) and injects the getter/setter bytecode directly into the .class file. Because of this, there is zero runtime performance overhead.

Enterprise Note: While @Data is great for DTOs, be careful using it on JPA @Entity classes. @Data generates equals() and hashCode() based on all fields, which can break Hibernate’s lazy loading if it triggers database queries. For Entities, prefer @Getter and @Setter instead of @Data.

Key Takeaways

  • ✅ Lombok generates getters, setters, constructors at compile-time — zero runtime overhead
  • ✅ @Data bundles @Getter, @Setter, @ToString, @EqualsAndHashCode
  • ✅ @Builder enables the Builder design pattern for cleaner object creation
  • ✅ @Slf4j auto-injects a logger variable into your class
  • ✅ Prefer @Getter/@Setter over @Data for JPA Entities to avoid lazy-loading issues