<?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: Andreas Eisele</title>
    <description>The latest articles on DEV Community by Andreas Eisele (@aeisele).</description>
    <link>https://dev.to/aeisele</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F272432%2Faa828ea4-3fb5-43e4-85ae-ac5a16dd1c67.png</url>
      <title>DEV Community: Andreas Eisele</title>
      <link>https://dev.to/aeisele</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aeisele"/>
    <language>en</language>
    <item>
      <title>Don't just blindly run mvn clean install...</title>
      <dc:creator>Andreas Eisele</dc:creator>
      <pubDate>Wed, 14 Oct 2020 11:17:53 +0000</pubDate>
      <link>https://dev.to/aeisele/don-t-just-blindly-run-mvn-clean-install-3f73</link>
      <guid>https://dev.to/aeisele/don-t-just-blindly-run-mvn-clean-install-3f73</guid>
      <description>&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;Believe me you almost never want to run the &lt;code&gt;install&lt;/code&gt; goal.&lt;/p&gt;

&lt;p&gt;If you want to create artifacts use this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn package
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;throw in the occasional &lt;code&gt;-D skipTests&lt;/code&gt; if you're feeling adventurous. 😉&lt;/p&gt;

&lt;p&gt;If you want to run tests (e.g. in a CI build) use this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn verify
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you're not convinced please keep reading.&lt;/p&gt;

&lt;h2&gt;
  
  
  about that maven install goal
&lt;/h2&gt;

&lt;p&gt;You see &lt;code&gt;mvn clean install&lt;/code&gt; has been handed around for years as the goto command to "build the software". But actually it is probably doing more than you are looking for.&lt;/p&gt;

&lt;h3&gt;
  
  
  we need to clean, or do we?
&lt;/h3&gt;

&lt;p&gt;First things first: why do we prefix the command with &lt;code&gt;clean&lt;/code&gt;? Well the target directory could hold outdated &lt;em&gt;stuff&lt;/em&gt; right? Can you actually remember a case where this was relevant? &lt;em&gt;Maven&lt;/em&gt; usually is smart enough to deal with whatever is left over in the target directory. Or should I say it is rather &lt;em&gt;"dumb"&lt;/em&gt; enough? &lt;em&gt;Maven&lt;/em&gt; tends to build more than is necessary because unlike &lt;em&gt;ye olde autotools&lt;/em&gt; it doesn't factor in file timestamps of source and target items to reduce the workload. One could argue that in the case of &lt;em&gt;java&lt;/em&gt; or &lt;em&gt;JVM&lt;/em&gt; based projects this would be a tough task anyway. Not every ".java" file maps clearly to a pre-known output file. Think about inner or anonymous classes.&lt;/p&gt;

&lt;p&gt;What the &lt;code&gt;clean&lt;/code&gt; goal is doing is to try to delete every target folder in every module of your project. Only for it to be created again immediately by the next lifecycle phase. So my advice would be &lt;strong&gt;if you don't necessarily know why you would use clean, just don't&lt;/strong&gt; it's gonna be fine in the most cases. &lt;/p&gt;

&lt;h3&gt;
  
  
  install always works, right?
&lt;/h3&gt;

&lt;p&gt;If you've ever had the pleasure of working with so called &lt;em&gt;"multi module"&lt;/em&gt; projects you know the pain. &lt;em&gt;Maven&lt;/em&gt; really isn't all that intuitive when it comes to how inter-project dependencies are resolved. Sooner or later someone will tell you "well duh, you need to install the dependencies to your local repo" and from that point of time on running the occasional &lt;code&gt;mvn clean install&lt;/code&gt; is just daily business.&lt;/p&gt;

&lt;p&gt;Take a look at the following simplified project structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J2t7lDhO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g1npu2xw934oxoyjf2is.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J2t7lDhO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g1npu2xw934oxoyjf2is.png" alt="a maven multi module project"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We have a root project (usually called "parent") and 3 child projects "core", "cli" and "web". We can also see that both "web" and "cli" depend on the "core" module. That's a pretty common structure where most of the code probably resides in the "core" module while different aspects like a web frontend in the web module might be relying on the core functionality.&lt;/p&gt;

&lt;p&gt;Now the tricky question: if you change something in the core module, will the dependant cli or web modules automatically pick up those changes on the next build? As everything in IT the answer to that question of course is: &lt;strong&gt;it depends!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The very unintuitive default for &lt;em&gt;maven&lt;/em&gt; is to look up dependencies in your local &lt;em&gt;maven&lt;/em&gt; repository. That is the &lt;code&gt;~/.m2/repostory&lt;/code&gt; thingy. This default also applies to dependencies if they happen to be located in the very same multi module project. Now the quick fix of course is to run &lt;code&gt;install&lt;/code&gt;. The problem with that of course is that now you have a fixed (snapshot?) version of your code built and copied to your local repo. This will not auto-update. Once you make further changes your dependant modules again will not &lt;em&gt;"see"&lt;/em&gt; the changes. This can lead to frustrating cycles of debugging, remembering that you forgot to do the &lt;em&gt;"install dance"&lt;/em&gt; and having to do the same thing all over again.&lt;/p&gt;

&lt;h3&gt;
  
  
  fire up that Reactor
&lt;/h3&gt;

&lt;p&gt;There is a way to tell &lt;em&gt;maven&lt;/em&gt; to actually look for dependencies in the local project first. &lt;em&gt;Maven&lt;/em&gt; does this by activating a thing called &lt;strong&gt;the Reactor&lt;/strong&gt;. The funny thing is that whether &lt;em&gt;maven&lt;/em&gt; actually uses the &lt;em&gt;reactor&lt;/em&gt; or not is by default bound to the goal (or rather lifecycle phase) that you are trying to execute. Goals like &lt;code&gt;install&lt;/code&gt;, &lt;code&gt;package&lt;/code&gt; and &lt;code&gt;verify&lt;/code&gt;use the &lt;em&gt;reactor&lt;/em&gt; by default. This is why you might have the impression that &lt;code&gt;install&lt;/code&gt; is the solution to your dependency problems. Goals like &lt;code&gt;compile&lt;/code&gt; on the other hand don't use the &lt;em&gt;reactor&lt;/em&gt; by default. They will gladly use outdated dependencies straight out of your local repo.&lt;/p&gt;

&lt;p&gt;So what if we want to build maybe only the web artifact of that example project mentioned above? We will need to activate the reactor, to get the latest &lt;code&gt;core.jar&lt;/code&gt; bundled into our &lt;code&gt;web.war&lt;/code&gt;. There are two ways to do this without doing an &lt;code&gt;install&lt;/code&gt; and therefore not littering our system with unnecessary artifacts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;we could run a complete package from the parent (building more than necessary):
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;root
mvn package
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;or we explicitly tell &lt;em&gt;maven&lt;/em&gt; to build the web project and its dependencies while activating the &lt;em&gt;reactor&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;root
mvn compile &lt;span class="nt"&gt;-pl&lt;/span&gt; web &lt;span class="nt"&gt;-am&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The take away here is that simply specifying a "project list" via &lt;code&gt;-pl&lt;/code&gt; is enough to use the reactor. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ever noticed the nice build summary &lt;em&gt;maven&lt;/em&gt; outputs by default? It's actually called a &lt;em&gt;reactor&lt;/em&gt; summary as you can see below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;INFO] Reactor Summary &lt;span class="k"&gt;for &lt;/span&gt;parent 0.0.1-SNAPSHOT:
&lt;span class="o"&gt;[&lt;/span&gt;INFO]
&lt;span class="o"&gt;[&lt;/span&gt;INFO] parent ............................................. SUCCESS &lt;span class="o"&gt;[&lt;/span&gt;  0.011 s]
&lt;span class="o"&gt;[&lt;/span&gt;INFO] core ............................................... SUCCESS &lt;span class="o"&gt;[&lt;/span&gt;  1.590 s]
&lt;span class="o"&gt;[&lt;/span&gt;INFO] cli ................................................ SUCCESS &lt;span class="o"&gt;[&lt;/span&gt;  1.518 s]
&lt;span class="o"&gt;[&lt;/span&gt;INFO] &lt;span class="nt"&gt;------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;INFO] BUILD SUCCESS
&lt;span class="o"&gt;[&lt;/span&gt;INFO] &lt;span class="nt"&gt;------------------------------------------------------------------------&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;INFO] Total &lt;span class="nb"&gt;time&lt;/span&gt;:  3.273 s
&lt;span class="o"&gt;[&lt;/span&gt;INFO] Finished at: 2020-10-13T18:09:21+02:00
&lt;span class="o"&gt;[&lt;/span&gt;INFO] &lt;span class="nt"&gt;------------------------------------------------------------------------&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Remarks
&lt;/h2&gt;

&lt;p&gt;I once had a blog post with the exact same title on Marco Behler's &lt;a href="https://www.marcobehler.com/blog"&gt;Blog&lt;/a&gt; back before the big redesign. By now Marco replaced that blog post with an extensive guide on Maven, you can find the link to that guide in the links section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;In dept guide about "mvn clean install" by &lt;a href="https://www.marcobehler.com/guides/mvn-clean-install-a-short-guide-to-maven"&gt;Marco Behler&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Photo by &lt;a href="https://unsplash.com/@itsmiki5?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Milan Popovic&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/craftsman?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Prevent Hibernate from doing N+1 selects with constructor expression</title>
      <dc:creator>Andreas Eisele</dc:creator>
      <pubDate>Sun, 27 Sep 2020 11:13:03 +0000</pubDate>
      <link>https://dev.to/aeisele/prevent-hibernate-from-doing-n-1-selects-with-constructor-expression-eak</link>
      <guid>https://dev.to/aeisele/prevent-hibernate-from-doing-n-1-selects-with-constructor-expression-eak</guid>
      <description>&lt;h2&gt;
  
  
  The Motivation
&lt;/h2&gt;

&lt;p&gt;While basic CRUD operations with &lt;em&gt;JPA&lt;/em&gt; / &lt;em&gt;Hibernate&lt;/em&gt; are easy, every application sooner or later needs to introduce &lt;em&gt;DTO&lt;/em&gt; style result objects for specific use cases like projections. That is part of the deal, no OR mapper can do your homework for you.&lt;/p&gt;

&lt;p&gt;Luckily &lt;em&gt;JPA&lt;/em&gt; gives us a way of specifying exactly what result objects we expect from a query. One way to do that is to use &lt;em&gt;JPQL&lt;/em&gt; with its so called 'constructor expression' in the select part of the query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select new com.example.NameDto(e.firstName, e.lastName) from Employee e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note the &lt;code&gt;new&lt;/code&gt; keyword and the fully qualified class name. This does exactly what you would think it would do. It creates a new instance of &lt;code&gt;NameDto&lt;/code&gt; using the appropriate public constructor for each result row.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Using parts of an entity and projecting them into a &lt;em&gt;DTO&lt;/em&gt; is fine and all but what happens if we want to fetch the whole entity and wrap it into a &lt;em&gt;DTO&lt;/em&gt;? Imagine the use case where we want to select an entity but also some auxiliary information that is not stored along with the entity. For example suppose we want to select "places near me in a 2000m radius". That's only one where condition and then you have a result set of the actual place entities right? What if we also want to show to the end user how far away from his position those places happen to be? Why don't we just wrap the &lt;code&gt;Place&lt;/code&gt; entity into an appropriate &lt;em&gt;DTO&lt;/em&gt; that also holds the distance, say &lt;code&gt;DistanceResult&lt;/code&gt;? Let's try it out.&lt;/p&gt;

&lt;p&gt;Here we have the &lt;code&gt;Place&lt;/code&gt; entity, you can see it has a location field indicating where it is located.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bNFrnKtw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gb9kzg50j34bvjyy5i42.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bNFrnKtw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gb9kzg50j34bvjyy5i42.PNG" alt="Place Entity"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And then we a &lt;em&gt;DTO&lt;/em&gt; called &lt;code&gt;DistanceResult&lt;/code&gt; which should just wrap our entity together with how far it is from the origin of the query.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--35NlpfOu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kcidvtlo32yc9s2qj3hb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--35NlpfOu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kcidvtlo32yc9s2qj3hb.PNG" alt="Distance Result DTO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Prepared with that let's fire a simple &lt;em&gt;JPQL&lt;/em&gt; query like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select new com.example.DistanceResult(p, distance(:center, p.location))
  from Place p
  where dwithin(:center, p.location, :radiusMeters) = true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ignore for a moment that we are dealing with geographic types and functions here. This is actually part of &lt;code&gt;hibernate-spatial&lt;/code&gt; and I use it on an instance of &lt;code&gt;postgis&lt;/code&gt; but this doesn't matter here.&lt;/p&gt;

&lt;p&gt;This is what our query log gives us for that query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;place1_&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;col_0_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;st_distance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;col_1_0_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;places&lt;/span&gt; &lt;span class="n"&gt;place0_&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;places&lt;/span&gt; &lt;span class="n"&gt;place1_&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;place1_&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;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;st_dwithin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;

&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;place0_&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;id1_0_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;location2_0_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&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;name3_0_0_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;places&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=?&lt;/span&gt;

&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;place0_&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;id1_0_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;location2_0_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&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;name3_0_0_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;places&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can clearly see that our singular &lt;em&gt;JPQL&lt;/em&gt; query resulted in one query that is roughly its &lt;em&gt;SQL&lt;/em&gt; equivalent. On top of that we can see additional selects one for each entity instance in our result&lt;br&gt;
set (here the result size is 2). This looks like the dreaded &lt;strong&gt;N+1&lt;/strong&gt; problem.&lt;/p&gt;
&lt;h2&gt;
  
  
  Solution 1: result transformers
&lt;/h2&gt;

&lt;p&gt;On one hand someone at the &lt;em&gt;hiberante&lt;/em&gt; project clearly had a reasoning behind why using the constructor expression would not fetch complete entities but rather only the immediate needed parts. On the other hand in the given use case N+1 is nothing we want to have. Sadly there is no clear way of telling hibernate to fetch the whole entity. For example a self join using the &lt;code&gt;fetch&lt;/code&gt; keyword doesn't work. Also the &lt;code&gt;fetch all properties&lt;/code&gt; stanza does not affect the resulting &lt;em&gt;SQL&lt;/em&gt; in this case.&lt;/p&gt;

&lt;p&gt;Luckily there is some other way to do the &lt;em&gt;DTO&lt;/em&gt; instantiation we want to have. This is &lt;em&gt;hibernate&lt;/em&gt; specific and called &lt;code&gt;ResultTransformer&lt;/code&gt;. Given any &lt;em&gt;hibernate&lt;/em&gt; query we can attach an instance of this interface and have our results handled by it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Result Transformers with Spring Data JPA
&lt;/h3&gt;

&lt;p&gt;I trust it a large part of &lt;em&gt;JPA&lt;/em&gt; and/or &lt;em&gt;hibernate&lt;/em&gt; users will probably be using &lt;em&gt;Spring Data JPA&lt;/em&gt; to avoid boilerplate. Here is how to use &lt;code&gt;ResultTransformer&lt;/code&gt; in that case:&lt;/p&gt;

&lt;p&gt;First we have our usual repository interface without anything fancy, note that it extends an additional interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;PlaceRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PagingAndSortingRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Place&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;DistancePlaceRepository&lt;/span&gt; &lt;span class="o"&gt;{&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 that additional interface, we can see our distance query method is defined here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;DistancePlaceRepository&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;DistanceResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findInDistance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radiusMeters&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But how is this method implemented? We can add another &lt;code&gt;@Repository&lt;/code&gt; bean into the context that implements only this&lt;br&gt;
interface. &lt;em&gt;Spring Data JPA&lt;/em&gt; will actually do some kind of &lt;em&gt;mixin&lt;/em&gt; magic behind the covers such that our concrete repository bean later on uses the following implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DistancePlaceRepositoryImpl&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;DistancePlaceRepository&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@PersistenceContext&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;EntityManager&lt;/span&gt; &lt;span class="n"&gt;entityManager&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@SuppressWarnings&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;&lt;span class="s"&gt;"unchecked"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"deprecation"&lt;/span&gt;&lt;span class="o"&gt;})&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&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;DistanceResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findInDistance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radiusMeters&lt;/span&gt;&lt;span class="o"&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;jpql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"select p, distance(:center, p.location) from Place p where dwithin(:center, p.location, :radiusMeters) = true"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&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;DistanceResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="n"&gt;entityManager&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createQuery&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jpql&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"center"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setParameter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"radiusMeters"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;radiusMeters&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unwrap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setResultTransformer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                        &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ListResultTransformer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;aliases&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DistanceResult&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                                        &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Place&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
                                        &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]).&lt;/span&gt;&lt;span class="na"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                &lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getResultList&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I'm using Vlad Mihalcea's &lt;code&gt;hibernate-types&lt;/code&gt; library here to get access to &lt;code&gt;ListResultTransformer&lt;/code&gt; (See links below).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now how does our query log look like if we use this transformer?&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;place0_&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;col_0_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;st_distance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;col_1_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&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;id1_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;location2_0_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&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;name3_0_&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;places&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;st_dwithin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;place0_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see its only a single select which is much better. No &lt;strong&gt;N+1&lt;/strong&gt; in sight.&lt;/p&gt;

&lt;h3&gt;
  
  
  Drawbacks
&lt;/h3&gt;

&lt;p&gt;The first thing you will note when trying to use &lt;code&gt;ResultTransformer&lt;/code&gt; is that the actual method to use them &lt;code&gt;Query#setResultTransformer&lt;/code&gt; is marked as deprecated. This is actually some form of premature deprecation by the &lt;em&gt;hibernate&lt;/em&gt; developers. Unless you're using &lt;em&gt;hibernate&lt;/em&gt; 6.0 there is no way around using this deprecated method. So it is clearly fine to ignore the warning here (suppress it until 6.0 is GA and then migrate).&lt;/p&gt;

&lt;p&gt;The second thing to note is that clearly this solution is &lt;em&gt;hibernate&lt;/em&gt; specific. &lt;code&gt;ResultTransformer&lt;/code&gt; is nothing &lt;em&gt;JPA&lt;/em&gt; has an equivalent of (yet). &lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 2: Blaze Persistence entity view
&lt;/h2&gt;

&lt;p&gt;While investigating this problem and related info on the internet I happened upon a stack overflow &lt;a href="https://stackoverflow.com/a/64041288/222272"&gt;answer&lt;/a&gt; leading me to discover so called &lt;em&gt;"Blaze Persistence Entity Views"&lt;/em&gt;. It looks like we could redefine our &lt;em&gt;DTO&lt;/em&gt; with this. I leave it as an exercise to the reader to find out via their &lt;a href="https://persistence.blazebit.com/documentation/1.6/entity-view/manual/en_US/#spring-data-features"&gt;documentation&lt;/a&gt; how exactly we would achieve the wrapping of the whole entity and also map the &lt;em&gt;distance&lt;/em&gt; part of our original query. Its an extensive library, I'm sure there is a way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 3: use jOOQ
&lt;/h2&gt;

&lt;p&gt;Last but not least I still have to mention that of course there is nothing preventing us from just mixing our existing &lt;em&gt;hibernate&lt;/em&gt; mapping with a little bit of &lt;a href="https://www.jooq.org/"&gt;jOOQ&lt;/a&gt; on the side. This would give us full control over what &lt;em&gt;SQL&lt;/em&gt; exactly we are executing. And we can easily wrap the resulting tuples into our &lt;em&gt;DTO&lt;/em&gt; if we want. Sure we would have to pay attention that the result are no managed entities but in our use case this isn't necessary in the first place.&lt;/p&gt;

&lt;p&gt;I said not to pay attention to the &lt;em&gt;spatial&lt;/em&gt; functions earlier but if we actually want to use &lt;em&gt;jOOQ&lt;/em&gt; for this kind of query then we would need support for those &lt;em&gt;SQL&lt;/em&gt; functions in the query builder. Luckily there is a project for this on github by the name of &lt;a href="https://github.com/dmitry-zhuravlev/jooq-postgis-spatial"&gt;jooq-postgis-spatial&lt;/a&gt; (in the case of postgis that is).&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Vlad Mihalcea on hibernate result transformers: &lt;a href="https://vladmihalcea.com/hibernate-resulttransformer"&gt;Blog Post&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Blaze Persistence entity views &lt;a href="https://persistence.blazebit.com/documentation/1.6/entity-view/manual/en_US/#spring-data-features"&gt;documentation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Original Question on &lt;a href="https://stackoverflow.com/questions/64028853/jpa-circumvent-n1-when-using-constructor-expression-in-jpql-to-wrap-singular-e"&gt;stackoverflow&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;jOOQ &lt;a href="https://www.jooq.org/"&gt;homepage&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Photo by &lt;a href="https://unsplash.com/@ninjason?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Jason Leung&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/drawer?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>hibernate</category>
      <category>jpa</category>
      <category>sql</category>
      <category>jpql</category>
    </item>
  </channel>
</rss>
