<?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: Arm Fahim</title>
    <description>The latest articles on DEV Community by Arm Fahim (@armfahim).</description>
    <link>https://dev.to/armfahim</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%2F4114240%2F5e9e6980-9e21-4848-92c4-51c6f377ac6e.jpg</url>
      <title>DEV Community: Arm Fahim</title>
      <link>https://dev.to/armfahim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/armfahim"/>
    <language>en</language>
    <item>
      <title>Optimizing Queries in Spring Data JPA</title>
      <dc:creator>Arm Fahim</dc:creator>
      <pubDate>Mon, 07 Sep 2026 15:53:25 +0000</pubDate>
      <link>https://dev.to/armfahim/optimizing-queries-in-spring-data-jpa-4b8f</link>
      <guid>https://dev.to/armfahim/optimizing-queries-in-spring-data-jpa-4b8f</guid>
      <description>&lt;p&gt;Spring Data JPA makes data access effortless — until a page that felt instant in development starts firing hundreds of queries in production. Here are the techniques I reach for most to keep queries fast and predictable.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Find and kill the N+1 problem
&lt;/h2&gt;

&lt;p&gt;The classic culprit. You load a list of entities, then touch a lazy association in a loop — and each iteration triggers another query. Ten orders with their line items become &lt;strong&gt;1 + 10 queries&lt;/strong&gt;. At scale, that's what turns a 50 ms endpoint into a 2-second one.&lt;/p&gt;

&lt;p&gt;The first step is simply to &lt;em&gt;see&lt;/em&gt; it. Turn on SQL logging in development:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;spring.jpa.show-sql&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="py"&gt;spring.jpa.properties.hibernate.format_sql&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;span class="py"&gt;logging.level.org.hibernate.SQL&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;DEBUG&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the same query repeating with different IDs, you've found an N+1.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Fetch joins — load associations in one query
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;JOIN FETCH&lt;/code&gt; tells Hibernate to pull the association in the same query instead of lazily loading it later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT o FROM Order o JOIN FETCH o.items WHERE o.status = :status"&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;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByStatusWithItems&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="s"&gt;"status"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One query, no surprises. Just be careful combining multiple collection fetches — it can produce a Cartesian product. For that, fetch one collection per query or use a &lt;code&gt;@BatchSize&lt;/code&gt; hint.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Entity graphs — reuse without hand-writing JPQL
&lt;/h2&gt;

&lt;p&gt;When you want the same eager-loading on a derived query, an &lt;code&gt;@EntityGraph&lt;/code&gt; keeps it declarative:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&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;"items"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"customer"&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;Order&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Don't fetch entities you only read
&lt;/h2&gt;

&lt;p&gt;For read-only endpoints (lists, reports, dropdowns) you rarely need full managed entities. A &lt;strong&gt;DTO projection&lt;/strong&gt; selects only the columns you use, skips the persistence context, and returns less data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;OrderSummary&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getCustomerName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;BigDecimal&lt;/span&gt; &lt;span class="nf"&gt;getTotal&lt;/span&gt;&lt;span class="o"&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;OrderSummary&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the highest-leverage changes for reporting screens — exactly the kind of high-concurrency workload where every skipped column adds up.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Always paginate large result sets
&lt;/h2&gt;

&lt;p&gt;Returning an unbounded list is a latent outage. Let Spring Data page for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Page&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;OrderSummary&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Pageable&lt;/span&gt; &lt;span class="n"&gt;pageable&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For deep pagination on huge tables, prefer &lt;strong&gt;keyset (cursor) pagination&lt;/strong&gt; over large &lt;code&gt;OFFSET&lt;/code&gt; values, which force the database to scan and discard rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Push work to the database — and index it
&lt;/h2&gt;

&lt;p&gt;Aggregations, filtering, and sorting belong in SQL, not in Java memory. And no query plan survives a missing index: make sure the columns in your &lt;code&gt;WHERE&lt;/code&gt; and &lt;code&gt;JOIN&lt;/code&gt; clauses are indexed, then confirm with &lt;code&gt;EXPLAIN&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rule of thumb: measure first. Turn on SQL logging, count the queries, then optimize the one that actually hurts — not the one you assume is slow.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://armfahim.com/blog/optimizing-spring-data-jpa-queries.html" rel="noopener noreferrer"&gt;armfahim.com&lt;/a&gt;.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>database</category>
      <category>performance</category>
    </item>
    <item>
      <title>Maintainable Reports with JasperReports Subreports</title>
      <dc:creator>Arm Fahim</dc:creator>
      <pubDate>Mon, 07 Sep 2026 15:52:33 +0000</pubDate>
      <link>https://dev.to/armfahim/maintainable-reports-with-jasperreports-subreports-poo</link>
      <guid>https://dev.to/armfahim/maintainable-reports-with-jasperreports-subreports-poo</guid>
      <description>&lt;p&gt;A single 40-band Jasper template that does everything becomes unmaintainable fast. Subreports let you compose reports from small, reusable pieces — here's how to wire them up without the usual headaches.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why subreports?
&lt;/h2&gt;

&lt;p&gt;A subreport is just a compiled report embedded inside another. Reach for them when you need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repeat a detailed block per record (e.g. an invoice with its line items).&lt;/li&gt;
&lt;li&gt;Reuse the same component — a header, a signature block — across many reports.&lt;/li&gt;
&lt;li&gt;Break one enormous template into pieces different people can own.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Passing parameters down
&lt;/h2&gt;

&lt;p&gt;The parent passes values to the child through &lt;code&gt;&amp;lt;subreportParameter&amp;gt;&lt;/code&gt;. Keep the parent's report parameters as the single source of truth and forward what the child needs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;subreport&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;reportElement&lt;/span&gt; &lt;span class="na"&gt;x=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt; &lt;span class="na"&gt;y=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"555"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"80"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;subreportParameter&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"INVOICE_ID"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;subreportParameterExpression&amp;gt;&lt;/span&gt;$F{id}&lt;span class="nt"&gt;&amp;lt;/subreportParameterExpression&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/subreportParameter&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;subreportExpression&amp;gt;&lt;/span&gt;$P{ITEMS_SUBREPORT}&lt;span class="nt"&gt;&amp;lt;/subreportExpression&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/subreport&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Give the subreport its own data
&lt;/h2&gt;

&lt;p&gt;There are two common ways to feed a subreport:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pass a data source&lt;/strong&gt; from the parent via &lt;code&gt;$P{REPORT_DATA_SOURCE}&lt;/code&gt; — best when the parent already has the data in memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pass a connection&lt;/strong&gt; and let the subreport run its own query — cleaner when each section maps to its own SQL.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compile the child to a &lt;code&gt;.jasper&lt;/code&gt; and pass it in as a parameter or a pre-loaded object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;JasperReport&lt;/span&gt; &lt;span class="n"&gt;itemsSub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;JasperCompileManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compileReport&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"items.jrxml"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ITEMS_SUBREPORT"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;itemsSub&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Pitfalls I learned the hard way
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compile every subreport.&lt;/strong&gt; A stale &lt;code&gt;.jrxml&lt;/code&gt;/&lt;code&gt;.jasper&lt;/code&gt; mismatch is the #1 cause of "why is it showing old data?".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch the width.&lt;/strong&gt; A subreport wider than the parent band silently overflows or clips. Match widths deliberately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Set "Print When Detail Overflows" / stretch&lt;/strong&gt; so a growing subreport pushes content down instead of overlapping.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache compiled reports.&lt;/strong&gt; Compiling on every request is expensive — compile once at startup and reuse.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;Reports are read-heavy, so the same rule from query optimization applies: fetch only the columns you render, do aggregation in SQL, and avoid running a fresh query per row inside a subreport when a single joined query would do.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Treat a report like code: small composable pieces, compiled artifacts under version control, and data-fetching that respects the database.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://armfahim.com/blog/jasperreports-subreports-guide.html" rel="noopener noreferrer"&gt;armfahim.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>jasperreports</category>
      <category>reporting</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
