title: Hibernate Native Query Error: Column not found
body: <p>I am using Java8 with Spring, Hibernate, JPA and MySQL.</p>
  
  <p>I have the following tables:</p>
  
  <pre><code> +----------+   +-----------------+    +-----------+
   |  PERSON  |   |  RATING_PERSON  |    |  RATING   |
   +----------+   +-----------------+    +-----------+
   |    ID    |   |     PER_ID      |    |    ID     |
   |          |   |     RAT_ID      |    |           |
   +----------+   +-----------------+    +-----------+
  </code></pre>
  
  <p>Then execute the following code:</p>
  
  <pre><code>    @Override
      public List&lt;Rating&gt; findByRatedBy(Long personId) {
          StringBuilder sb = new StringBuilder();     
          sb.append(" SELECT * FROM ebdb.rating as r ");
          sb.append(" WHERE r.ID = (SELECT rp.RAT_ID ");
          sb.append(" from ebdb.rating_person as rp where rp.PER_ID = :perId) ");
  //      sb.append(" SELECT r.* FROM ebdb.rating as r ");
  //      sb.append(" INNER JOIN ebdb.rating_person as rp ON r.ID = rp.RAT_ID ");
  //      sb.append(" WHERE rp.PER_ID = :perId ");
          Query q = entityManager.createNativeQuery(sb.toString(), Rating.class);
          q.setParameter("perId", personId);
          List&lt;Rating&gt; ratings = (List&lt;Rating&gt;) q.getResultList();
          return ratings;
      }
  </code></pre>
  
  <p>But get the following error:</p>
  
  <blockquote>
  <pre><code>WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-7) SQL Error: 0, SQLState: S0022
  ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-7) Column 'PER_ID' not found.
  </code></pre>
  </blockquote>
  
  <p>When I run the exact same SQL statement in MySQLWorkbench, it executes with no problems.</p>
  
  <pre><code> SELECT * FROM ebdb.rating as r  WHERE r.ID = (SELECT rp.RAT_ID  from ebdb.rating_person as rp where rp.PER_ID = 385) 
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Can anyone please advise how I can get the Hibernate Native Query to execute this SQL?</p>
  
  <p>Thanks</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried removing the <code>Rating.class</code>, i.e. changing:</p>
  
  <pre><code>Query q = entityManager.createNativeQuery(sb.toString(), Rating.class);
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>Query q = entityManager.createNativeQuery(sb.toString());
  </code></pre>
  
  <p>This partially solves the problem. </p>
  
  <p>I do get a result set:</p>
  
  <pre><code>List&lt;Rating&gt; ratings = (List&lt;Rating&gt;) q.getResultList();
  </code></pre>
  
  <p>The value of <code>ratings</code>:</p>
  
  <pre><code>[[Ljava.lang.Object;@5b5c9618]
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IMJAG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IMJAG.png" alt="enter image description here"></a></p>
  
  <p>However, when I try use the <code>ratings</code>:</p>
  
  <pre><code>for (Rating rating : ratings) {
     ...
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
  <pre><code>[Ljava.lang.Object; cannot be cast to com.jobs.spring.domain.Rating
  </code></pre>
  </blockquote>
  
  <p><strong>More Info:</strong></p>
  
  <p><strong>Rating.java</strong></p>
  
  <pre><code>@Entity
  @Table(name="rating")
  @XmlRootElement(name="rating")
  public class Rating extends AbstractDomain&lt;Long&gt; {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = "STARS", nullable = false)
      private Long rating;    
  
      @Size(min=0, max=500)
      @Column(name = "REVIEW", nullable = false)
      private String review;
  
      @Column(name = "REVIEW_DATE", nullable = false)
      private Long reviewDate;
  
      @ManyToOne(fetch=FetchType.EAGER)
      @JoinTable
      (
          name="rating_job",
          joinColumns={ @JoinColumn(name="RAT_ID", referencedColumnName="ID") },
          inverseJoinColumns={ @JoinColumn(name="JOB_ID", referencedColumnName="ID") }
      )
      private Job job;
  
      @ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
      @JoinTable
      (
          name="rating_person",
          joinColumns={ @JoinColumn(name="RAT_ID", referencedColumnName="ID") },
          inverseJoinColumns={ @JoinColumn(name="PER_ID", referencedColumnName="ID") }
      )
      private Person person;
  
      @Column(name = "ANONYMOUS", nullable = false)
      private Integer anonymous;
  
      @XmlElement
      public Integer getAnonymous() {
          return anonymous;
      }
  
      public void setAnonymous(Integer anonymous) {
          this.anonymous = anonymous;
      }
  
      @XmlElement
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      @XmlElement
      public Long getRating() {
          return rating;
      }
  
      public void setRating(Long rating) {
          this.rating = rating;
      }
  
      @XmlElement
      public String getReview() {
          return review;
      }
  
      public void setReview(String review) {
          this.review = review;
      }
  
      @XmlElement
      public Long getReviewDate() {
          return reviewDate;
      }
  
      public void setReviewDate(Long reviewDate) {
          this.reviewDate = reviewDate;
      }
  
      @XmlElement
      public Job getJob() {
          return job;
      }
  
      public void setJob(Job job) {
          this.job = job;
      }
  
      @XmlElement
      public Person getPerson() {
          return person;
      }
  
      public void setPerson(Person person) {
          this.person = person;
      }
  }
  </code></pre>