title: Spring JPA DTO projection and handling the nested projection with null values
body: <p>I am using class based projection with constructor expressions. here is a sample code form my work</p>
  <pre><code>@Query(&quot;select new com.core.data.category.CategoryDto(c.id,c.code,c.externalCode,c.seoMeta, c.createdAt, c.updatedAt,c.parent.code) FROM Category c where c.code = :code&quot;)
  CategoryDto findCategoryByCode(@Param(&quot;code&quot;) String code); 
  </code></pre>
  <p>This is how my <code>CategoryDto</code> look like:</p>
  <pre><code>public class CategoryDto implements Serializable {
   
  private Long id;
  private String code;
  private String externalCode;
  private SEOMeta seoMeta;
  private CategoryDto parent;
  private LocalDateTime createdAt;
  private LocalDateTime updatedAt;
  Map&lt;String, LocCategoryDto&gt; translation;
  
  //constructor based on the requirement
  }
  </code></pre>
  <p>This seems to be working fine except in one case where the property for the nested object is <code>null</code>. in my case the parent property can be <code>null</code> in case this is a root category but it seems using <code>c.parent.code</code> causing the issue and the entire object is coming <code>null</code>. Can someone help me with the following queries</p>
  <ol>
  <li>Is there a way to handle this case using the same constructor expressions? I tried to look in to the doc but did not find the details.</li>
  <li>I think other option might be using ResultTransformer (which will bind my code to specific JPA)but I did not find any information as how I can use it with Spring JPA.</li>
  </ol>
  <p><strong>Update</strong>
  I even tried the option to use the <code>CASE</code> option but seems this is also not working for me as I am still getting the null entity (while data is available in the DB). Here is the updated code I tried</p>
  <pre><code>@Query(value = &quot;select new com.core.data.category.CategoryDto(c.id,c.code,c.externalCode,c.seoMeta, c.createdAt, c.updatedAt, &quot; +
              &quot;CASE &quot; +
              &quot;WHEN  c.parent is NULL &quot; +
              &quot;THEN NULL &quot; +
              &quot;ELSE c.parent.code &quot; +
              &quot;END ) &quot; +
              &quot;FROM Category c where c.code = :code&quot;)
      CategoryDto findCategoryByCode(@Param(&quot;code&quot;) String code);
  </code></pre>
  <p><strong>Edit 2</strong>
  I had also tried even with join but that also seems not working for.</p>
  <p><strong>Update</strong>: I did a silly mistake.Was using the simple join and not left join which caused this issue.</p>