title: Selecting an entity from another table using a Hibernate formula
body: <p>I'm attempting to retrieve an entity from another table using Hibernate's @Formula annotation. Given the following code:</p>
  
  <pre><code>@Entity
  class Test {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", updatable = false, nullable = false)
      private Long id = null;
  
      // ...
  
      @Formula("(SELECT r FROM Result r WHERE test_id = id AND resultDate = (SELECT MAX(resultDate) FROM Result WHERE test_id = id))")
      private Result lastestResult;
  
      // ...
  
      public Result getLatestResult() {
          return latestResult;
      }
  
      // ...
  }
  </code></pre>
  
  <p>The Result class looks like this:</p>
  
  <pre><code>@Entity
  class Result {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", updatable = false, nullable = false)
      private Long id = null;
  
      @ManyToOne
      private Test test;
  
      // ...
  }
  </code></pre>
  
  <p>But upon attempting to load a Test entity, I'm getting the error 'Column "TEST0_.RESULTDATE" not found'. I've tried a few other things as well involving the @JoinFormula annotation, but without any success. I also came across <a href="https://stackoverflow.com/questions/7098722/hibernate-load-entities-with-formula">this answer</a>, which seems to indicate that what I'm doing should work, but it doesn't. How do I make this work?</p>