title: Exception when selecting specific columns using Hibernate and Spring Data JPA
body: <p>I have a table that has a bytea column (named 'pdf') and I don't want to always select it, specially when I'm returning a list from the database, due to performance issues.</p>
  
  <p>I use native queries with spring data inside the repository to solve these types of situations before (when I used eclipselink), but with Hibernate, if I don't write all the columns in the query, it throws an exception.</p>
  
  <p>For test purposes, I'm trying to select only the <em>id</em> from the User and I still get the exception.</p>
  
  <p>Example: "SELET user.id FROM user WHERE user.id = '1'"</p>
  
  <p>It throws an exception saying that it did not find <em>name</em> in the ResultSet, if I put <em>name</em> in the SQL, it then says <em>age</em> was not found and so on, until I have to write all the columns in the SQL.</p>
  
  <p>Thanks in advance for any help.</p>
  
  <hr>
  
  <p><strong>What I have tried already</strong>:</p>
  
  <ul>
  <li>Updating/Downgrading Hibernate and Spring Data with no luck.</li>
  <li>Creating a new entity with only the columns I need, works, but it's a messy solution for me.</li>
  </ul>
  
  <p>Maybe the problem is the combination of the frameworks I use and the way I use them, if someone wants, I could try to upload my whole project structure.</p>
  
  <hr>
  
  <p><strong>My code</strong>:
  <em>Entity</em></p>
  
  <pre><code>@Entity
  @Table(name = "user", schema = "portal")
  public class User implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "id")
  private Integer id;
  
  @Column(name = "pdf")
  private byte[] pdf;
  
  @Column(name = "name")
  private String name;
  
  @Column(name = "age")
  private Integer age;
  
  public User() {
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public byte[] getPdf() {
      return pdf;
  }
  
  public void setPdf(byte[] pdf) {
      this.pdf = pdf;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public Integer getAge() {
      return age;
  }
  
  public void setAge(Integer age) {
      this.age = age;
  }    
  
  @Override
  public int hashCode() {
      int hash = 0;
      hash += (id != null ? id.hashCode() : 0);
      return hash;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Anexo)) {
          return false;
      }
      Anexo other = (Anexo) object;
      if ((this.id == null &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id))) {
          return false;
      }
      return true;
  }
  
  @Override
  public String toString() {
      return "br.gov.to.secad.portal.domain.User[ id=" + id + " ]";
  }
  
  }
  </code></pre>
  
  <hr>
  
  <p><em>Service</em></p>
  
  <pre><code>@Service
  @Transactional(readOnly = true)
  public class UserService implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Autowired
  private IUserRepository userRepository;
  
  
  public UserService() {
  }
  
  
  public User findOne() {
      return userRepository.findOneSQL();
  }
  
  }
  </code></pre>
  
  <hr>
  
  <p><em>Repository</em></p>
  
  <pre><code>public interface IUserRepository extends JpaRepository&lt;User, Serializable&gt;, JpaSpecificationExecutor {
  
  @Query(value = "SELECT user.id FROM user WHERE user.id = '1'", nativeQuery = true)
  public User findOneSQL();
  
  }
  </code></pre>
  
  <hr>
  
  <p><strong>The exception:</strong></p>
  
  <pre><code>org.postgresql.util.PSQLException: The column name name was not found in this ResultSet.
  </code></pre>
  
  <hr>
  
  <p><strong>Solution</strong></p>
  
  <p>The solution is using an array of Object when I want to select anything less than what I've mapped on my Entity class, thats the limitation of Hibernate that I now understand. </p>
  
  <p>So basically, the method will return Object[] and then I can iterate each position and instantiate a new entity of User with these values.</p>
  
  <p>Example: </p>
  
  <pre><code>@Query(value = "SELECT user.id FROM user WHERE user.id = '1'", nativeQuery = true)
  public Object[] findOneSQL();
  </code></pre>