title: Spring Data JPA Projection selected fields from the DB
body: <p>I was testing Spring Data 1.10.4.RELEASE, following the example in Spring Data Docs <a href="http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections" rel="noreferrer">http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections</a></p>
  
  <p>and I noticed some issues for which I have 2 questions.</p>
  
  <p>First let's suppose I have these 2 entities:</p>
  
  <pre><code>@Entity
  public class Person {
  
    @Id @GeneratedValue
    private Long id;
    private String firstName, lastName;
  
    @OneToOne
    private Address address;
  }
  
  @Entity
  public class Address {
  
    @Id @GeneratedValue
    private Long id;
    private String street, state, country;
  }
  </code></pre>
  
  <ul>
  <li><strong>Question 1:</strong> </li>
  </ul>
  
  <p>for the following projections:</p>
  
  <pre><code>interface PersonLimited {  
  
    String getFirstName(); 
  
    AddressLimited getAddress();
  }
  
  interface AddressLimited {  
  
    String getCountry(); 
  }
  </code></pre>
  
  <p>when I run <em>findPersonByFirstNameProjectedForLimitedData</em></p>
  
  <pre><code>interface PersonRepository extends CrudRepository&lt;Person, Long&gt; {
  
    @Query("select p from Person p where p.firstName = ?1")
    PersonLimited findPersonByFirstNameProjectedForLimitedData(String firstName);
  }
  </code></pre>
  
  <p>it returns exactly what expected :</p>
  
  <pre><code>{
      firstName: 'Homer',
      address: {
          country: 'USA'
      }
  }
  </code></pre>
  
  <p>now if I look into the generated SQL, this is what I have:</p>
  
  <pre><code>SELECT person0_.firstName      AS col_0_0_, 
         address1_.id            AS id1_13_, 
         address1_.street        AS street2_13_, 
         address1_.state         AS state3_13_, 
         address1_.country       AS country4_13_
  FROM   person person0_ 
         LEFT OUTER JOIN address address1_ 
                      ON person0_.addressId = address1_.id 
  WHERE  person0_.firstName = ?  
  </code></pre>
  
  <p>The projection for the "Person" entity is selecting only "fistName", which is 100% correct because in the PersonLimited interface I've only defined "getFirstName".</p>
  
  <p>But for the "Address" entity, it selects all the fields, which is wrong because in the AddressLimited interface I've only defined "getCountry", It should only select "country".</p>
  
  <p><strong>The generated query should be something like:</strong></p>
  
  <pre><code>SELECT person0_.firstName      AS col_0_0_, 
         address1_.country       AS country4_13_
  FROM   person person0_ 
         LEFT OUTER JOIN address address1_ 
                      ON person0_.addressId = address1_.id 
  WHERE  person0_.firstName = ?  
  </code></pre>
  
  <p>so the question is, why it is not selecting only the "country" field for the Address "entity"? why it needs to select all the fields? is it a bug in Spring?</p>
  
  <ul>
  <li><strong>Question 2:</strong></li>
  </ul>
  
  <p>for the same projection as above,</p>
  
  <p>when I run <em>findAllPersonsProjectedForLimitedData</em></p>
  
  <pre><code>interface PersonRepository extends CrudRepository&lt;Person, Long&gt; {
  
    @Query("select p from Person p")
    List&lt;PersonLimited&gt; findAllPersonsProjectedForLimitedData();
  }
  </code></pre>
  
  <p>it returns exactly what expected :</p>
  
  <pre><code>[
       {
          firstName: 'Homer',
          address: {
              country: 'USA'
          }
       },
       {
          firstName: 'Maggie',
          address: {
              country: 'USA'
          }
       }
  ]
  </code></pre>
  
  <p>now if I look into the generated SQL, this is what I have:</p>
  
  <pre><code>SELECT person0_.id                 AS id1_18_, 
         person0_.firstName          AS firstName2_18_, 
         person0_.lastName           AS lastName3_18_, 
         person0_.addressid          AS company4_18_
  FROM   person person0_ 
  
  SELECT address0_.id         AS id1_13_0_, 
         address0_.street     AS street2_13_0_, 
         address0_.state      AS state3_13_0_, 
         address0_.country    AS country4_13_0_
  FROM   address address0_ 
  WHERE  address0_.id = ? 
  </code></pre>
  
  <p>here, the projection for both the Person and the Address entities is selecting all the fields which is wrong, it should only select "firstName" and "country".</p>
  
  <p><strong>The generated query should be something like:</strong></p>
  
  <pre><code>SELECT person0_.firstName        AS firstName2_18_
  FROM   person person0_ 
  
  SELECT address0_.country    AS country4_13_0_
  FROM   address address0_ 
  WHERE  address0_.id = ? 
  </code></pre>
  
  <p>is this the normal behavior, shouldn't select only the fields that we need?</p>
  
  <p>Thanks,</p>