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
๐Ÿš€Spring Boot: Use @EntityGraph to control fetch strategies and prevent N 1 queries. #SpringBoot #SoftwareEngineering
2
8
76
2,005
๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฆ๐—ผ๐—น๐˜ƒ๐—ฒ ๐—ก ๐Ÿญ ๐—ฃ๐—ฟ๐—ผ๐—ฏ๐—น๐—ฒ๐—บ ๐Ÿญ. ๐—จ๐˜€๐—ฒ ๐—™๐—˜๐—ง๐—–๐—› ๐—๐—ข๐—œ๐—ก (๐—•๐—ฒ๐˜€๐˜ & ๐— ๐—ผ๐˜€๐˜ ๐—จ๐˜€๐—ฒ๐—ฑ) @๐˜˜๐˜ถ๐˜ฆ๐˜ณ๐˜บ("๐˜š๐˜Œ๐˜“๐˜Œ๐˜Š๐˜› ๐˜ฐ ๐˜๐˜™๐˜–๐˜” ๐˜–๐˜ณ๐˜ฅ๐˜ฆ๐˜ณ ๐˜ฐ ๐˜‘๐˜–๐˜๐˜• ๐˜๐˜Œ๐˜›๐˜Š๐˜ ๐˜ฐ.๐˜ช๐˜ต๐˜ฆ๐˜ฎ๐˜ด") ๐˜“๐˜ช๐˜ด๐˜ต<๐˜–๐˜ณ๐˜ฅ๐˜ฆ๐˜ณ> ๐˜ง๐˜ช๐˜ฏ๐˜ฅ๐˜ˆ๐˜ญ๐˜ญ๐˜ž๐˜ช๐˜ต๐˜ฉ๐˜๐˜ต๐˜ฆ๐˜ฎ๐˜ด(); 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
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
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
Custom JPA Queries and Optimization with Spring Data JPA: This Java example demonstrates Spring Data JPA's advanced features for database operations. It uses @Query for a custom JPQL query to fetch users by status, @EntityGraph to optimize fetching of related entities, and @CreatedDate/@LastModifiedDate for automatic auditing of entity creation and modification timestamps. These features enhance query flexibility, performance, and maintainability in a Spring-based application.
2
15
109
8,847
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.
13
62
3,542
2. [@EntityGraph to Fix N 1] This loads authors and their books in a single query. Bye-bye N 1! #ayshriv #java #springboot
1
1
24
1,544
1. [@OneToMany LAZY Load Warning] Accessing books without a fetch strategy in a loop causes N 1 problem. Fix it using JOIN FETCH or @EntityGraph.
4
32
1,886
โœ… Spring Boot JPA H2 @EntityGraph #java #springboot #jpa #h2 #entitygraph
2
13
172
11,157
Advanced Database Tuning โœ… Databases are often the biggest bottleneck in improving API performance. Even if you have indexes set, inefficient data fetching strategies and queries can lead to bad performance. What techniques can help here? - Using a single query, we can use JPAโ€™s @ EntityGraph or JPQL JOIN FETCH to load the related entities eagerly. We can also use DTO projections to fetch only the required fields. - Using Batching for bulk inserts and updates. This saves network trips by combining multiple SQL statements at a time. - Use Read Replicas if your use case is read-heavy.
1
3
54
2,206
N 1 Query Problem? Use @EntityGraph to fetch related entities efficiently instead of multiple queries. #ayshriv #java #springboot
5
50
2,008
Avoid LazyInitializationException in Spring Data JPA by using @Transactional or fetching relationships eagerly with @EntityGraph. #SpringJPA #Hibernate #ayshriv #java
2
35
1,161
Eu geralmente uso essas regras: 1- JPA รฉ รณtimoโ€ฆ atรฉ nรฃo ser mais: Quando o projeto cresce, vocรช precisa ir alรฉm do bรกsico. 2- JPQL sรณ para queries simples. Para relatรณrios pesados, joins complexos ou filtros dinรขmicos demais: Usar Native Queries com SQL puro. 3- Use JOIN FETCH e EntityGraph para evitar o famoso problema do N 1 em consultas 4- Transaรงรตes longas sรฃo inimigas da performance. 5- Nunca usar JPA pra operaรงรตes em massa. 6- Precisa sรณ de 3 campos? Nรฃo carregue o objeto inteiro. 7- Considerar Spring Data JDBC quando: vocรช tem domรญnios mais simples e um controle maior sobre o banco. Em tempo: Uma combinaรงรฃo de JPA procedures/functions diretas no banco pode ser mais valioso do que diversas dicas acima. Deixe o banco trabalhar por vocรช e nรฃo o spring.
5
134
The package org.hibernate.graph was long overdue for a complete overhaul. It's now been re-engineered, which fixed a number of holes in our implementation of JPA's EntityGraph facility.
1
228
Ever aince Hibernate 2, the way to avoid cartesian products in eager fetching is SUBSELECT fetching. This is sadly not available with JPA's EntityGraph, but it is available via @FetchProfile.
1
1
11
752
London Clojurians Talk: Introducing EntityGraph: Ergonomic Data Store for SPA State (by Geo Grigoryan) youtu.be/G-KOJyoLWrg Discussions: discu.eu/q/youtu.be/G-KOJyoLโ€ฆ #clojure #programming

1
4
201