title: Spring JPA/Hibernate Repository findAll is doing N+1 requests instead of a JOIN by default in Kotlin
body: <p>I am working in a Spring JPA/Hibernate application with Kotlin and I want to find all elements in an entity.</p>
  <p>That entity has a foreign key with a @ManyToOne relationship. I want to get all elements with their associated values with a JOIN query avoiding the N+1 problem.</p>
  <p>One thing is that the foreign keys are not related to the primary keys, but to another unique field in the entities (UUID).</p>
  <p>I was able to make that query with a JOIN creating a custom Query with a JOIN FETCH, but my point is to avoid creating those queries and make those JOINS in all findAlls by default.</p>
  <p>Is that possible or do I have to make a query in JPQL manually to force the JOIN FETCH?</p>
  <p>Here is the example code:</p>
  <pre><code>    @Entity
      data class A {
        @Id
        val id: Long,
      
        @Column
        val uuid: UUID,
      
        @Column
        val name: String
      }
  </code></pre>
  <pre><code>    @Entity
      data class B {
        @Id
        val id: Long,
      
        ...
      
        @Fetch(FetchMode.JOIN)
        @ManyToOne
        @JoinColumn(name = &quot;a_uuid&quot;, referencedColumnName = &quot;uuid&quot;, insertable = false, updatable = false)
        val a: A
      }
  </code></pre>
  <pre><code>    @Repository
      interface Repo&lt;B&gt; : CrudRepository&lt;B, Long&gt;
  </code></pre>
  <pre><code>   ...
     repo.findAll() // &lt;-- This triggers N+1 queries instead of making a JOIN
     ...
  
  </code></pre>