title: Spring Data JPA: Work with Pageable but with a specific set of fields of the entity
body: <p>I am working with <code>Spring Data</code> 2.0.6.RELEASE.</p>
  
  <p>I am working about <em>pagination</em> for performance and presentation purposes.
  Here about performance I am talking about that if we have a lot of records is better show them through pages</p>
  
  <p>I have the following and works fine:</p>
  
  <pre><code>interface PersonaDataJpaCrudRepository extends PagingAndSortingRepository&lt;Persona, String&gt; {
  }
  </code></pre>
  
  <p>The <code>@Controller</code> works fine with:</p>
  
  <pre><code>@GetMapping(produces=MediaType.TEXT_HTML_VALUE)
  public String findAll(Pageable pageable, Model model){
  </code></pre>
  
  <p>Through <code>Thymeleaf</code> I am able to apply pagination. Therefore until here the goal has been accomplished.</p>
  
  <p><strong>Note:</strong> The <code>Persona</code> class is annotated with <code>JPA</code> (<code>@Entity</code>, <code>Id</code>, etc)</p>
  
  <p>Now I am concerned about the following: even when pagination works in <code>Spring Data</code> about the amount the records, what about of the content of each record?. </p>
  
  <p>I mean: let's assume that <code>Persona</code> class contains 20 fields (consider any entity you want for your app), thus for a view based in <code>html</code> where a <em>report</em> only uses 4 fields (id, firstname, lastname, date), thus we have 16 unnecessary fields <em>for each</em> entity in memory</p>
  
  <p>I have tried the following:</p>
  
  <pre><code>interface PersonaDataJpaCrudRepository extends PagingAndSortingRepository&lt;Persona, String&gt; {
  
      @Query("SELECT p.id, id.nombre, id.apellido, id.fecha FROM Persona p")
      @Override
      Page&lt;Persona&gt; findAll(Pageable pageable);
  
  }
  </code></pre>
  
  <p>If I do a simple print in the <code>@Controller</code> it fails about the following:</p>
  
  <pre><code>java.lang.ClassCastException: 
  [Ljava.lang.Object; cannot be cast to com.manuel.jordan.domain.Persona
  </code></pre>
  
  <p>If I avoid that the view fails with:</p>
  
  <pre><code>Caused by: 
  org.springframework.expression.spel.SpelEvaluationException: 
  EL1008E: 
  Property or field 'id' cannot be found on object of type 
  'java.lang.Object[]' - maybe not public or not valid?
  </code></pre>
  
  <p>I have read many posts in SO such as:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/44285188/java-lang-classcastexception-ljava-lang-object-cannot-be-cast-to">java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to</a></li>
  </ul>
  
  <p>I understand the answer and I am agree about the <code>Object[]</code> return type because I am working with specific set of fields.</p>
  
  <ol>
  <li>Is mandatory work with the complete set of fields for each entity? Should I simply accept the cost of memory about the 16 fields in this case that never are used? It for each record retrieved?</li>
  <li>Is there a solution to work around with a specific set of fields or <code>Object[]</code> with the current API of <code>Spring Data</code>?</li>
  </ol>