Filter
Exclude
Time range
-
Near
Avoid raw JOIN FETCH hardcoding in repository queries, as it forces static mapping. Instead, leverage dynamic JPA Entity Graphs via @EntityGraph. Define your targeted fetching paths programmatically at the service boundary level. #SQL
1
26
Yeah... the idea has come of age, the proof will be when this scales i.e. adoption and evidence of clarity / meaning. #EntityGraph
69
Replying to @mario_casari
@entitygraph is slept on. one annotation and hibernate stops firing a query per row.
1
1
36
๐Ÿš€Spring Boot: Use @EntityGraph to control fetch strategies and prevent N 1 queries. #SpringBoot #SoftwareEngineering
2
8
76
2,005
ไปŠๅ›žใฎๅŠ ็ญ†ใง่ฟฝๅŠ ใ—ใŸไธปใชใƒˆใƒ”ใƒƒใ‚ฏ: ใƒปTransactional ใฎ propagation ๅˆคๆ–ญๅŸบๆบ–ใ€readOnly ใฎๅฎŸๅŠนๆ€ง ใƒปไพ‹ๅค– vs Resultๅž‹ ใฎไฝฟใ„ๅˆ†ใ‘ ใƒปN 1 ๅ•้กŒใฎ่งฃๆฑบ็ญ–ๆฏ”่ผƒ(JOIN FETCH / EntityGraph / Batch Fetch) ใƒปH2 vs Testcontainers ใฎๅˆคๆ–ญ่ปธ ใƒป็พๅ ดใง่ตทใใŸไบ‹ๆ•…ไบ‹ไพ‹
1
38
En JPA, TOUTES les relations doivent รชtre LAZY. โ€‹Rappel des valeurs par dรฉfaut : โœ… @OneToMany / @ManyToMany : Lazy (bon comportement) โŒ @ManyToOne / @OneToOne : Eager (le piรจge classique) โ€‹Pourquoi forcer le LAZY partout ? Une relation EAGER est chargรฉe ร  chaque requรชte, mรชme lorsque vous n'en avez pas besoin. Rรฉsultat : requรชtes SQL superflues, surconsommation de mรฉmoire et apparition du fameux problรจme N 1. La rรจgle : @ManyToOne(fetch = FetchType.LAZY) @OneToOne(fetch = FetchType.LAZY) Chargez ensuite explicitement les donnรฉes nรฉcessaires via @EntityGraph ou JOIN FETCH. โ€‹Principe d'architecture ร  retenir : Le chargement d'une relation doit รชtre une dรฉcision explicite du code appelant, et non un comportement cachรฉ ou subi depuis l'entitรฉ.
56
Le problรจme N 1 en JPA/Hibernate : Vous chargez N entitรฉs, puis vous accรฉdez ร  une relation lazy (@OneToMany, @ManyToOne). Hibernate dรฉclenche 1 requรชte pour la liste, puis 1 requรชte par entitรฉ pour charger la relation. 100 commandes avec leurs lignes โ†’ 101 requรชtes SQL au lieu d'1 seule. 3 faรงons de l'รฉviter, de la plus utilisรฉe ร  la moins utilisรฉe : โ†’ @EntityGraph sur la mรฉthode du repository : Dรฉclaratif, lisible, directement sur le Spring Data Repository. Solution par dรฉfaut aujourd'hui. @EntityGraph(attributePaths = "items") โ†’ JOIN FETCH en JPQL : Requรชte explicite qui charge la relation en un seul SELECT avec JOIN. Utile dans les requรชtes custom. SELECT o FROM Order o JOIN FETCH o.items โ†’ Projection JPA : Si vous n'avez pas besoin des entitรฉs complรจtes, sรฉlectionnez directement les champs utiles via un DTO ou une interface de projection. Plus performant, moins de mรฉmoire, mais demande plus de code. Comment le dรฉtecter : โ†’ spring.jpa.show-sql=true en dรฉveloppement pour voir les requรชtes gรฉnรฉrรฉes โ†’ Hibernate Statistics pour compter les requรชtes par transaction Rรจgle simple : Si le nombre de requรชtes dรฉpend de la taille du rรฉsultat, c'est un problรจme N 1. #Java #SpringBoot #Hibernate #Performance
45
Every Software Engineer need to know about N 1 Query Problem Imagine you fetch 10 usersโ€ฆ Then for EACH user, you fetch their posts. 1 query (users) 10 queries (posts) = 11 queries ๐Ÿ˜ฌ This is called the N 1 problem As you are making multiple database call List<User> users = userRepository.findAll(); for (User user : users) { List<Post> posts = postRepository.findByUserId(user.getId()); } Solution 1: JOIN FETCH (JPA) @Query("SELECT u FROM User u JOIN FETCH u.posts") List<User> users = userRepository.findAllWithPosts(); Fetch users posts in ONE query Solution 2: EntityGraph @EntityGraph(attributePaths = {"posts"}) List<User> findAll(); Solution 3: Batch Fetching (Hibernate) spring.jpa.properties.hibernate.default_batch_fetch_size=10 Reduces queries by batching Solution 4: Eager Loading But it can lead to extra data Pro Tip: Use JOIN FETCH for small datasets Use batching for large datasets Rule: "Never query inside a loop" #Java #Backend #SpringBoot #SystemDesign #Performance

1
22
๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฆ๐—ผ๐—น๐˜ƒ๐—ฒ ๐—ก ๐Ÿญ ๐—ฃ๐—ฟ๐—ผ๐—ฏ๐—น๐—ฒ๐—บ ๐Ÿญ. ๐—จ๐˜€๐—ฒ ๐—™๐—˜๐—ง๐—–๐—› ๐—๐—ข๐—œ๐—ก (๐—•๐—ฒ๐˜€๐˜ & ๐— ๐—ผ๐˜€๐˜ ๐—จ๐˜€๐—ฒ๐—ฑ) @๐˜˜๐˜ถ๐˜ฆ๐˜ณ๐˜บ("๐˜š๐˜Œ๐˜“๐˜Œ๐˜Š๐˜› ๐˜ฐ ๐˜๐˜™๐˜–๐˜” ๐˜–๐˜ณ๐˜ฅ๐˜ฆ๐˜ณ ๐˜ฐ ๐˜‘๐˜–๐˜๐˜• ๐˜๐˜Œ๐˜›๐˜Š๐˜ ๐˜ฐ.๐˜ช๐˜ต๐˜ฆ๐˜ฎ๐˜ด") ๐˜“๐˜ช๐˜ด๐˜ต<๐˜–๐˜ณ๐˜ฅ๐˜ฆ๐˜ณ> ๐˜ง๐˜ช๐˜ฏ๐˜ฅ๐˜ˆ๐˜ญ๐˜ญ๐˜ž๐˜ช๐˜ต๐˜ฉ๐˜๐˜ต๐˜ฆ๐˜ฎ๐˜ด(); This loads orders items in single query ๐Ÿฎ. ๐—จ๐˜€๐—ฒ @๐—˜๐—ป๐˜๐—ถ๐˜๐˜†๐—š๐—ฟ๐—ฎ๐—ฝ๐—ต @๐˜Œ๐˜ฏ๐˜ต๐˜ช๐˜ต๐˜บ๐˜Ž๐˜ณ๐˜ข๐˜ฑ๐˜ฉ(๐˜ข๐˜ต๐˜ต๐˜ณ๐˜ช๐˜ฃ๐˜ถ๐˜ต๐˜ฆ๐˜—๐˜ข๐˜ต๐˜ฉ๐˜ด = {"๐˜ช๐˜ต๐˜ฆ๐˜ฎ๐˜ด"}) ๐˜“๐˜ช๐˜ด๐˜ต<๐˜–๐˜ณ๐˜ฅ๐˜ฆ๐˜ณ> ๐˜ง๐˜ช๐˜ฏ๐˜ฅ๐˜ˆ๐˜ญ๐˜ญ(); ๐Ÿฏ. ๐—จ๐˜€๐—ฒ ๐—ณ๐—ฒ๐˜๐—ฐ๐—ต = ๐—™๐—ฒ๐˜๐—ฐ๐—ต๐—ง๐˜†๐—ฝ๐—ฒ.๐—˜๐—”๐—š๐—˜๐—ฅ (โš ๏ธ ๐—ก๐—ผ๐˜ ๐—ฅ๐—ฒ๐—ฐ๐—ผ๐—บ๐—บ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ฑ ๐—”๐—น๐˜„๐—ฎ๐˜†๐˜€) @OneToMany(fetch = FetchType.EAGER) Can create huge data load and memory issues ๐Ÿฐ. ๐—จ๐˜€๐—ฒ ๐—•๐—ฎ๐˜๐—ฐ๐—ต ๐—™๐—ฒ๐˜๐—ฐ๐—ต๐—ถ๐—ป๐—ด (๐—›๐—ถ๐—ฏ๐—ฒ๐—ฟ๐—ป๐—ฎ๐˜๐—ฒ ๐˜€๐—ฝ๐—ฒ๐—ฐ๐—ถ๐—ณ๐—ถ๐—ฐ) ๐˜ฉ๐˜ช๐˜ฃ๐˜ฆ๐˜ณ๐˜ฏ๐˜ข๐˜ต๐˜ฆ.๐˜ฅ๐˜ฆ๐˜ง๐˜ข๐˜ถ๐˜ญ๐˜ต_๐˜ฃ๐˜ข๐˜ต๐˜ค๐˜ฉ_๐˜ง๐˜ฆ๐˜ต๐˜ค๐˜ฉ_๐˜ด๐˜ช๐˜ป๐˜ฆ=10 Instead of N queries, Hibernate groups them into batches ๐Ÿฑ. ๐—จ๐˜€๐—ฒ ๐——๐—ง๐—ข ๐—ฃ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป (๐—•๐—ฒ๐˜€๐˜ ๐—ณ๐—ผ๐—ฟ ๐—”๐—ฃ๐—œ๐˜€) @๐˜˜๐˜ถ๐˜ฆ๐˜ณ๐˜บ("๐˜š๐˜Œ๐˜“๐˜Œ๐˜Š๐˜› ๐˜ฏ๐˜ฆ๐˜ธ ๐˜ค๐˜ฐ๐˜ฎ.๐˜ฅ๐˜ต๐˜ฐ.๐˜–๐˜ณ๐˜ฅ๐˜ฆ๐˜ณ๐˜‹๐˜›๐˜–(๐˜ฐ.๐˜ช๐˜ฅ, ๐˜ช.๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ) ๐˜๐˜™๐˜–๐˜” ๐˜–๐˜ณ๐˜ฅ๐˜ฆ๐˜ณ ๐˜ฐ ๐˜‘๐˜–๐˜๐˜• ๐˜ฐ.๐˜ช๐˜ต๐˜ฆ๐˜ฎ๐˜ด ๐˜ช") List<OrderDTO> fetchData(); ๐Ÿง  Interview Answer (Perfect) If interviewer asks: ๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—ก ๐Ÿญ ๐—ฝ๐—ฟ๐—ผ๐—ฏ๐—น๐—ฒ๐—บ ๐—ถ๐—ป ๐—๐—ฃ๐—”? Say this: N 1 problem occurs when one query loads parent entities and additional N queries are fired to fetch child collections lazily. It leads to performance issues and is solved using fetch join, entity graph, or batch fetching.
5
1
9
5,291
Lazy vs Eager Loading in JPA Same relationship. Different fetch strategy. Huge performance impact. Letโ€™s understand this with a simple restaurant example ๐Ÿฝ๏ธย  Imagine you ordered ONE tea. ๐Ÿ”ด FetchType.EAGERย  You ordered one tea.ย ย  But the waiter brought tea snacks dessert together.ย  Thatโ€™s EAGER loading. When the parent entity is fetched,ย ย  all related entities are loaded immediately โ€”ย  Even if they are not required. Good for small relationships.ย ย  Risky for large collections. ๐ŸŸข FetchType.LAZYย  You ordered one tea.ย ย  The waiter brought only tea.ย  Thatโ€™s LAZY loading. Related entities are loaded only when accessed.ย  More controlled.ย ย  Usually better for performance. โš ๏ธ The N 1 Query Problemย  Suppose you fetch 4 users. Query 1 โ†’ Fetch usersย ย  Query 2 โ†’ Orders for User 1ย ย  Query 3 โ†’ Orders for User 2ย ย  Query 4 โ†’ Orders for User 3ย ย  Query 5 โ†’ Orders for User 4ย  Total = 1 N queriesย  1 query loads parent entities.ย ย  N additional queries load child data.ย  More database round trips.ย ย  Lower performance. ๐Ÿ›  How to Fix Itย  โœ” Use JOIN FETCHย ย  โœ” Use @EntityGraphย ย  โœ” Use DTO projectionย  Fetch associations explicitlyย ย  to avoid excessive queries. Golden Rule ๐Ÿง ย  EAGER loads immediately.ย ย  LAZY loads on demand.ย  Performance depends on *when* you fetch,ย ย  not just *what* you fetch. Interview Questions I Was Asked: Q1. Why is @ManyToOne EAGER by default?ย ย  A. Because it typically references a single small entity. Q2. What causes the N 1 problem?ย ย  A. Lazy loading inside loops without proper join fetching. Q3. Why is LAZY usually preferred?ย ย  A. It prevents unnecessary joins and reduces memory usage.
1
7
5,142
N 1 ๋ฌธ์ œ๋Š” ORM์„ ์‚ฌ์šฉํ•˜๋Š” ํ•œ ํ‰์ƒ ๋”ฐ๋ผ๋‹ค๋‹ˆ๋Š” ์ˆ™์ œ์™€ ๊ฐ™๋‹ค. ๋‚˜๋Š” ์ด ๊ธ€์—์„œ ์ œ์‹œํ•˜๋Š” ํ•ด๊ฒฐ์ฑ…๋“ค์— ๊ณต๊ฐํ•˜๋ฉด์„œ๋„, ๊ฒฐ๊ตญ ๊ฐœ๋ฐœ์ž๊ฐ€ ์‹คํ–‰๋˜๋Š” SQL์„ ์ง์ ‘ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋Š” ์Šต๊ด€์„ ๊ฐ–๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํ™•์‹คํ•œ ์˜ˆ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ์ถ”์ƒํ™” ์ˆ˜์ค€์ด ๋†’์•„์งˆ์ˆ˜๋ก ๋‚ด๋ถ€ ๋™์ž‘์„ ๋†“์น˜๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ์‹ค๋ฌด์—์„œ Fetch Join๊ณผ EntityGraph ์ค‘ ์–ด๋–ค ๋ฐฉ์‹์„ ๋” ์„ ํ˜ธํ•˜๋Š”์ง€, ํ˜น์€ ์ž์‹ ๋งŒ์˜ ํ•ด๊ฒฐ ๋…ธํ•˜์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•˜๋‹ค. medium.com/@sunil17bbmp/spriโ€ฆ #SpringBoot #JPA

26
500 Abfragen oder eine? Wer #JPA & Spring Data falsch einsetzt, landet im N 1-Desaster. Julius Mischok zeigt, wie du mit #EntityGraph JPQL JPA von Bremse zu Boost wechselst. ๐Ÿ‘‰ Wenig Aufwand, viel Speed โ€“ jetzt in der #JAVAPRO: javapro.io/de/high-speed-jpaโ€ฆ #Hibernate #CleanCode
3
63
Replying to @vlad_mihalcea
This is out of date. In JPA 3.2 you should never use untypesafe hints to pass an EntityGraph. Also, you should use the static metamodel to obtain references to named entity graphs in a completely typesafe way.
1
2
14
2,035
6 Dec 2025
Replying to @iamsaquibdev
get LazyInitializationException, for solved the problem we can use @transactional(readOnly=true) or entitygraph or join fetch
1
31
Replying to @BhanuPr28630948
Error: LazyInitializationException: could not initialize proxy - no Session. The Hibernate session closes after findById() returns. Accessing lazy-loaded orders outside the session fails. We can use @ Transactional, JOIN FETCH, or @ EntityGraph
1
9
1,202
#JPA langsam? Nur wenn du es falsch nutzt. Julius Mischok zeigt, wie du N 1-Fallen, Lazy-Traps & Flushing-Probleme erkennst & mit @EntityGraph JPQL echten Speed rausholst. ๐Ÿ‘‰ Lerne jetzt 5 konkrete Optimierungsmuster: javapro.io/de/high-speed-jpaโ€ฆ @hibernate @springboot #JAVAPRO
2
3
171
One of the most asked Java/backend interview questions in an interview โ‡’The N 1 Problem in JPA โ†’ 1 initial query: To fetch N parent entities. โ†’ N subsequent queries: For each of the N parent entities, a separate query is executed to fetch its related children. โ†’ Results in 1 N database queries, causing inefficiency and slow response, performance issue Eg: Fetching 100 Authors(1 query) and then, in a loop, getting author.getBooks() for each(N=100), leads to 101 queries. โ‡’How to Resolve N 1 in JPA โ‡† Goal is to fetch associated data in fewer queries, usually in a single round trip. โ†’JPQL/HQL JOIN FETCH (Recommended for specific joins): Explicitly tells JPA to fetch associations using a JOIN in your query. Eg: @Query("SELECT a FROM Author a JOIN FETCH a.books") List<Author> findAllWithBooks(); Benefit: Single, efficient query. Caution: Can lead to Cartesian products if joining multiple collections. โ†’Batch Fetching (@BatchSize or hibernate.default_batch_fetch_size): Instead of one query per parent, fetches associated collections for a batch of parent IDs in a single query. Eg: @BatchSize(size = 10) @OneToMany(...) private List<Book> books; Benefit: Reduces 1 N to 1 (N / batch_size) queries, avoiding Cartesian products. โ†’Entity Graphs (@EntityGraph - Spring Data JPA / JPA 2.1 ): Declaratively defines which associations to fetch eagerly for a specific repository method. Eg: @EntityGraph(attributePaths = "books") List<Author> findAll(); Benefit: Flexible, reusable fetching strategies without explicit JPQL joins. Conclusion: Always use explicit fetching strategies (like JOIN FETCH, @BatchSize, or EntityGraph) to address N 1 issues and ensure efficient database interaction
2
34
311
14,863
7 Sep 2025
Java: Spring Data JPA Features:โœจ๏ธ Spring Data JPA enhances database operations with @Query for custom JPQL, @EntityGraph for fetch optimization, and auditing via @CreatedDate and @LastModifiedDate. -> Executes custom JPQL to fetch users by status using @Param. -> Eagerly fetches department to optimize data retrieval. -> Auto-tracks creation/update timestamps with @CreatedDate and @LastModifiedDate. -> Simplifies custom queries, fetching, and auditing for efficient JPA usage.
26