<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: programming coyote</title>
    <description>The latest articles on DEV Community by programming coyote (@programming_coyote).</description>
    <link>https://dev.to/programming_coyote</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4068860%2F1872a84d-cf8e-476f-9971-3f88418658cf.png</url>
      <title>DEV Community: programming coyote</title>
      <link>https://dev.to/programming_coyote</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/programming_coyote"/>
    <language>en</language>
    <item>
      <title>Understanding and Solving the N+1 Problem in Spring Data JPA / Hibernate</title>
      <dc:creator>programming coyote</dc:creator>
      <pubDate>Sun, 09 Aug 2026 16:52:01 +0000</pubDate>
      <link>https://dev.to/programming_coyote/understanding-and-solving-the-n1-problem-in-spring-data-jpa-hibernate-2jn</link>
      <guid>https://dev.to/programming_coyote/understanding-and-solving-the-n1-problem-in-spring-data-jpa-hibernate-2jn</guid>
      <description>&lt;ul&gt;
&lt;li&gt;If you are working with ORM (Object-Relational Mapping) tools like Hibernate or Spring Data JPA, there is a high chance you will run into a common performance issue called the N+1 Problem.

&lt;/li&gt;
&lt;li&gt;Whether you work with Java, Node.js (Prisma), Python (Django), or C# (.NET), this concept applies to almost every modern backend framework.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🧐 What is the N+1 Problem?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The N+1 problem occurs when an ORM executes 1 initial query to fetch a list of records, and then executes N additional queries to fetch related data for each item in that list.

&lt;/li&gt;
&lt;li&gt;Instead of fetching all the required data in one single query, your application ends up making $1 + N$ queries to the database.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ⚙️ When Does It Happen?
&lt;/h3&gt;

&lt;p&gt;The N+1 problem usually happens when three conditions are met:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have &lt;strong&gt;two or more related database tables&lt;/strong&gt; (e.g., Marks and Student).

&lt;/li&gt;
&lt;li&gt;You define entity relationships like &lt;strong&gt;@ManyToOne&lt;/strong&gt;, &lt;strong&gt;@OneToMany&lt;/strong&gt;, or &lt;strong&gt;@ManyToMany&lt;/strong&gt;.

&lt;/li&gt;
&lt;li&gt;The relationship uses &lt;strong&gt;Lazy Loading&lt;/strong&gt; &lt;strong&gt;(FetchType.LAZY)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  💡 Step-by-Step Example
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let’s say we have two entities: &lt;strong&gt;MarksEntity&lt;/strong&gt; and &lt;strong&gt;StudentEntity&lt;/strong&gt;. Each mark belongs to a student (&lt;code&gt;@ManyToOne&lt;/code&gt;).

&lt;/li&gt;
&lt;li&gt;We want to retrieve &lt;strong&gt;10 mark records&lt;/strong&gt; along with the student details for each mark.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Here is what happens behind the scenes with &lt;em&gt;Lazy Loading&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. First Query (1)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Data JPA fetches 10 records from the marks table.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Creating Proxy (Dummy) Objects&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because &lt;em&gt;student&lt;/em&gt; is set to &lt;em&gt;LAZY&lt;/em&gt; loading, Hibernate does not fetch the student data immediately. Instead, it puts a &lt;strong&gt;Proxy (Dummy) Object&lt;/strong&gt; inside the mark entity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Subsequent Queries (N = 10)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, in your service layer or DTO mapper, you loop through the marks and access the student’s name: &lt;em&gt;mark.getStudent().getName()&lt;/em&gt;.

&lt;/li&gt;
&lt;li&gt;The moment you call &lt;em&gt;getStudent()&lt;/em&gt;, Hibernate realizes the actual student data is missing. It fires an &lt;strong&gt;extra query&lt;/strong&gt; to fetch the student for that specific mark. Since there are 10 marks, it fires &lt;strong&gt;10 extra queries&lt;/strong&gt;!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;…&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  📊 Total Queries Sent to the Database:
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Total Queries = 1 (Initial Query) + 10 (Student Queries) = 11 Queries&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  🛑 Why is this bad for performance?
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;If you fetch 10,000 records in production, your application will fire 10,001 queries to the database. This causes high network latency, heavy CPU usage, and slows down your entire application&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h4&gt;
  
  
  🛠️ How to Fix the N+1 Problem
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;To fix this, we need to tell the database: “Do not create dummy objects. Join the tables and fetch all the required data in a single query.”&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;In Spring Data JPA, there are two primary ways to do this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Method 1&lt;/strong&gt;: &lt;strong&gt;&lt;em&gt;@EntityGraph (Declarative / Annotation Method)&lt;/em&gt;&lt;/strong&gt; 🏆&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 2&lt;/strong&gt;: &lt;strong&gt;&lt;em&gt;JOIN FETCH (Explicit JPQL Method)&lt;/em&gt;&lt;/strong&gt; 🥈&lt;/p&gt;




&lt;h4&gt;
  
  
  Method 1: @EntityGraph (Declarative / Annotation Method) 🏆
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;This is the easiest and cleanest way provided by Spring Data JPA. You do not need to write custom JPQL queries. You simply add the &lt;strong&gt;@EntityGraph&lt;/strong&gt; annotation above your repository method and specify which relationships to fetch eagerly.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Overriding the built-in findAll method&lt;/span&gt;
&lt;span class="nd"&gt;@EntityGraph&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attributePaths&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"student"&lt;/span&gt;&lt;span class="o"&gt;})&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MarksEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using with a custom derived query&lt;/span&gt;
&lt;span class="nd"&gt;@EntityGraph&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;attributePaths&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"student"&lt;/span&gt;&lt;span class="o"&gt;})&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MarksEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findBySubject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🟢 Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clean and simple&lt;/strong&gt;: No need to write manual &lt;em&gt;SQL or JPQL queries&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pagination friendly&lt;/strong&gt;: Works seamlessly with Pageable (&lt;strong&gt;&lt;em&gt;Page &amp;lt; MarksEntity&amp;gt;&lt;/em&gt;&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-level joins&lt;/strong&gt;: You can easily fetch nested relationships (e.g., &lt;strong&gt;&lt;em&gt;attributePaths = {"student", "student.school"}&lt;/em&gt;&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  Method 2: JOIN FETCH (Explicit JPQL Method) 🥈
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;This method involves writing a custom JPQL query using the &lt;strong&gt;JOIN FETCH&lt;/strong&gt; keyword. It tells Hibernate to join the table and populate the related entity fields immediately.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using JOIN FETCH in a custom JPQL query&lt;/span&gt;
&lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="no"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="no"&gt;FROM&lt;/span&gt; &lt;span class="nc"&gt;MarksEntity&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="no"&gt;JOIN&lt;/span&gt; &lt;span class="no"&gt;FETCH&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;student&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MarksEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findAllWithStudent&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using JOIN FETCH with a WHERE clause&lt;/span&gt;
&lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="no"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="no"&gt;FROM&lt;/span&gt; &lt;span class="nc"&gt;MarksEntity&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="no"&gt;JOIN&lt;/span&gt; &lt;span class="no"&gt;FETCH&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="no"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MarksEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByStudentCity&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Param&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🟢 Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Full control&lt;/strong&gt;: Perfect when you need complex logic involving &lt;strong&gt;&lt;em&gt;WHERE&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;ORDER BY&lt;/em&gt;&lt;/strong&gt;, or &lt;strong&gt;&lt;em&gt;GROUP BY&lt;/em&gt;&lt;/strong&gt; clauses.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  🔍 Do Both Methods Generate the Same SQL Query?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Yes!&lt;/strong&gt; Both &lt;strong&gt;@EntityGraph&lt;/strong&gt; and &lt;strong&gt;JOIN FETCH&lt;/strong&gt; generate the exact same SQL &lt;strong&gt;INNER JOIN&lt;/strong&gt; (or &lt;strong&gt;LEFT OUTER JOIN&lt;/strong&gt;) query under the hood:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;mark_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  💡 Best Practices Comparison
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Recommended Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Simple finder methods or&lt;/strong&gt; &lt;code&gt;findAll()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;@EntityGraph&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Paginated queries&lt;/strong&gt; (&lt;code&gt;Pageable&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;@EntityGraph&lt;/code&gt; &lt;em&gt;(JOIN FETCH can cause issues with in-memory pagination)&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Complex queries with custom&lt;/strong&gt; &lt;code&gt;WHERE&lt;/code&gt; &lt;strong&gt;clauses&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;JOIN FETCH&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h4&gt;
  
  
  ⚠️ Why Not Just Use FetchType.EAGER?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;You might wonder: &lt;em&gt;Why not set &lt;strong&gt;@ManyToOne(fetch = FetchType.EAGER)&lt;/strong&gt; directly on the Entity field?&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Never set FetchType.EAGER globally on your entities!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;If you set it to &lt;strong&gt;&lt;em&gt;EAGER&lt;/em&gt;&lt;/strong&gt;, Hibernate will &lt;strong&gt;always&lt;/strong&gt; fetch the related data, even in endpoints where you don't need it. This degrades overall application performance.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  🎯 Golden Rule
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Always keep entity relationships set to &lt;strong&gt;FetchType.LAZY&lt;/strong&gt; by default.

&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;@EntityGraph&lt;/strong&gt; or &lt;strong&gt;JOIN FETCH&lt;/strong&gt; on specific repository queries whenever you actually need the related data.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>softwaredevelopment</category>
      <category>database</category>
      <category>java</category>
    </item>
  </channel>
</rss>
