DEV Community

Cover image for 🎯 I Was Fetching Entire Entities… Until I Learned About Projections
Shashwath S H
Shashwath S H

Posted on

🎯 I Was Fetching Entire Entities… Until I Learned About Projections

When I started using Spring Data JPA, my approach was simple:

Need data?
Fetch the entity.

Every time.

It worked.
But something felt wrong.

APIs returned too much data.
Performance started degrading.
Clients received fields they never asked for.

That’s when I discovered Spring Data JPA Projections — and everything changed.


🧠 What Are Projections in Spring Data JPA?

Projections allow you to fetch only the data you actually need from the database.

Instead of loading the entire entity:

  • You select specific fields
  • You reduce query overhead
  • You return lighter responses

In short:

Projections help you avoid over-fetching data.


⚠️ The Problem with Fetching Full Entities

Fetching full entities:

  • Loads unnecessary columns
  • Increases memory usage
  • Makes APIs slower
  • Exposes more data than required

This may be fine for small apps — but it doesn’t scale.


🧩 Types of Projections in Spring Data JPA

Spring Data JPA supports three main types of projections.

Each one solves a slightly different problem.


1️⃣ Interface-Based Projections (Most Common)

This is the most popular and cleanest way to use projections.

You define an interface with getter methods for required fields.

Spring Data JPA automatically maps query results to it.

Why this is powerful:

  • No implementation needed
  • Clean and readable
  • Works seamlessly with repositories

Perfect when:

  • You want partial data
  • You don’t want extra objects

2️⃣ Class-Based (DTO) Projections

Here, you define a DTO class and use a constructor to map values.

This is useful when:

  • You want custom logic
  • You want computed fields
  • You need strong control over response structure

Slightly more verbose — but very flexible.


3️⃣ Open Projections (Advanced but Dangerous)

Open projections allow:

  • Derived values
  • Expression-based fields

They look powerful — but there’s a catch.

⚠️ They may trigger additional queries, impacting performance.

Use them carefully and only when needed.


🔍 How Spring Data JPA Makes This Work

Under the hood:

  • Spring analyzes your repository method return type
  • Detects projection usage
  • Generates optimized queries
  • Maps results automatically

You don’t write SQL.
You don’t write mapping code.

This is convention over configuration done right.


🛠️ Projections with Repository Methods

Projections work naturally with:

  • Query methods
  • Derived queries
  • Sorting
  • Pagination

Which means:
👉 You can combine Projections + Pageable + Sort for production-ready APIs.


⚠️ The Mistake I Was Making

I used to:

  • Always return entities
  • Let Jackson decide response shape
  • Ignore payload size

It worked… until APIs slowed down.

After switching to projections:

  • Responses became lighter
  • Performance improved
  • APIs felt cleaner and safer

🚀 Final Thoughts

Spring Data JPA Projections are not a micro-optimization.

They are a design decision.

If your Spring Boot APIs:

  • Return too much data
  • Feel slow
  • Expose unnecessary fields

Start with Projections.

This post is part of my learning-in-public journey while exploring Spring Boot and real-world backend optimization.

Top comments (0)