Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Intelligence View
⚡ tsecurity.de Intelligence

Spring JPA - @EntityGraph

By default, Spring Data JPA loads related data lazily, which can cause the N+1 problem — one query for the main data and more for each related item. @EntityGraph annotation fixes this by letting you load related data in a single query, w…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

By default, Spring Data JPA loads related data lazily, which can cause the N+1 problem — one query for the main data and more for each related item. @EntityGraph annotation fixes this by letting you load related data in a single query, without changing your entity mapping. It’s simple, improves performance, and avoids writing custom JOIN FETCH queries.






Example



Let’s say you have a Post entity that references an Author.



Entities




@Entity
public class Post {
@Id
private Long id;
private String title;

@ManyToOne(fetch = FetchType.LAZY)
private Author author;
}

@Entity
public class Author {
@Id
private Long id;
private String name;
}






Repository




public interface PostRepository extends JpaRepository<Post, Long> {

// attributePaths specifies related entities to eagerly load
@EntityGraph(attributePaths = {"author"})
List<Post> findAll();
}






Now, if you create a simple controller to return all posts and have SQL logging enabled, you'll see the queries printed in the console.



With @EntityGraph, only one query runs to fetch both the post and its author. Without it, multiple queries are executed — one for the posts and one for each author — which is the N+1 problem.



✅ Using @EntityGraph

Only one query — fast and efficient.




select p.id, p.title, a.id, a.name
from post p
left join author a on p.author_id = a.id






🚨 Not Using @EntityGraph

N+1 queries — slow and wasteful.




select * from post;
select * from author where id = ?;
select * from author where id = ?;


Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Spring JPA - @EntityGraph

Thematisch verwandte Begriffe: Spring, EntityGraph · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-101045 | Fleet-maintained app install and uninstall scripts for macOS are genera…
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag