title: Hibernate @JoinFormula
body: <p>I have two entities A and B.</p>
  
  <pre><code> public class A{
  
      @Id
      @GeneratedValue
      private Integer id;
  
      private String uuid;
  
      ...
    }
  </code></pre>
  
  <p>The UUID is given externally; ID can be seen as version.</p>
  
  <p>Now, I'd like to reference A in B such that I have the uuid stored in B and automatically select the A with the according uuid and the highest id.</p>
  
  <p>What I tried is:</p>
  
  <pre><code>public class B{
        @Id 
        @GeneratedValue
        private Integer id;
  
        private String uuidOfA;
  
        @ManyToOne
        @JoinFormula(value="SELECT a.id FROM A a WHERE v.uuid = uuidOfA AND v.id = (SELECT max(x.id) FROM A x WHERE x.uuid = v.uuid)", referencedColumnName="id")
        private A a; 
  
        ...        
  }
  </code></pre>
  
  <p>This will create a column containing A's id in B and throws an exception if I try to persist an object. I've also tried @JoinColumnsOrFormulas without luck.</p>
  
  <p>Can someone give me a hint on how to do this (in Hibernate 3.5 btw)?</p>
  
  <p>Thanks!</p>