title: Hibernate's @JoinFormula to retrieve a list of childs causes ClassCastException
body: <p>I runned into a problem that happens when trying to use the <code>@JoinFormula</code> annotation on a <code>@OneToMany</code> property of an entity.</p>
  <p>I need to map the childs of a entity using a custom join (for now, let's say, all the childs, just to simplify the upcoming example).</p>
  <h2>My attempt</h2>
  <p>In this case, what makes sense to me is to do:</p>
  <p><strong>Approach A</strong></p>
  <p>Parent entity:</p>
  <pre><code>@Entity
  @Table(name=&quot;parent&quot;)
  public class Parent {
      @Id
      @GeneratedValue(strategy= GenerationType.AUTO)
      private Long id;
  
      @OneToMany(mappedBy=&quot;parent&quot;)
      @JoinFormula(value=&quot;(SELECT * FROM child)&quot;)
      private Set&lt;Child&gt; childs;
      
      //getters, setters...
  }
  </code></pre>
  <p>Child entity:</p>
  <pre><code>@Entity
  @Table(name=&quot;child&quot;)
  public class Child {
      @ManyToOne()
      private Parent parent;
  
      //Ids, getters, setters...
  }
  </code></pre>
  <p>So, this code works well when no Join Formula is annotated, but, when it is, i get this while booting my Spring JPA application:</p>
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not instantiate collection persister org.hibernate.persister.collection.OneToManyPersister
  [...]
  Caused by: java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column
  </code></pre>
  <p>Basically, I want to do <a href="https://vladmihalcea.com/how-to-map-the-latest-child-of-a-parent-entity-using-hibernate-joinformula/" rel="nofollow noreferrer">this</a> but without returning only one child, being able to return a list of childs.</p>
  <h2>My research</h2>
  <ul>
  <li>The solution seems to be <a href="https://discourse.hibernate.org/t/how-to-use-the-hibernate-joinformula-annotation-with-a-onetomany-jpa-association/1461/2?u=carlos_lopez" rel="nofollow noreferrer">moving the @JoinFormula to the @ManyToOne side and make the @OneToMany bidirectional</a>, which would be something like:</li>
  </ul>
  <p><strong>Approach B</strong></p>
  <p>Parent entity:</p>
  <pre><code>@Entity
  @Table(name=&quot;parent&quot;)
  public class Parent {
      @OneToMany(mappedBy=&quot;parent&quot;)
      private Set&lt;Child&gt; childs;
  
      //Ids, getters, setters...
  }
  </code></pre>
  <p>Child entity:</p>
  <pre><code>@Entity
  @Table(name=&quot;child&quot;)
  public class Child {
  
      @ManyToOne()
      @JoinFormula(value=&quot;(SELECT * FROM parent)&quot;) //I guess you don't have to change the FROM clause. Tried both anyway.
      private Parent parent;
  
      //Ids, getters, setters...
  }
  </code></pre>
  <ul>
  <li><p>But that throws a similar exception:</p>
  <pre><code>   Exception in thread &quot;main&quot; java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column
  </code></pre>
  </li>
  <li><p>According to <a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=1044758&amp;view=next" rel="nofollow noreferrer">the Hibernate's team</a> it's a problem with a Spring Boot related class.</p>
  </li>
  <li><p>I did open a <a href="https://github.com/spring-projects/spring-boot/issues/18533" rel="nofollow noreferrer">issue on Spring Boot's GitHub</a>, and they replicated the example above without any Spring Boot class involved and still happens the same exception, so it is definitely <strong>not a Spring problem</strong> as they pointed.</p>
  </li>
  </ul>
  <h2>Conclusion.</h2>
  <p>Personally I conclude that <a href="https://hibernate.atlassian.net/browse/HHH-12997" rel="nofollow noreferrer">hibernate ignores this bug</a>.</p>