title: Using hibernate @Formula to fetch a collection
body: <p>I have a similar db structure. The only difference that I have more tables on the path from A to C entity:</p>
  <p><a href="https://i.stack.imgur.com/A1Fkm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A1Fkm.png" alt="enter image description here" /></a></p>
  <p>And I have following mapping for this structure:</p>
  <pre><code>@Entity
  @Table(name = &quot;a&quot;)
  class A {
      @Id
      private int id;
  
      private String title;
  
      @ElementCollection(targetClass=String.class)
      @Formula(&quot;(select (c.useful_information) from A a &quot; +
              &quot;join B b on a.id = b.a_id &quot; +
              &quot;join C c on b.id = c.b_id &quot; +
              &quot;where a.id = id)&quot;)
      private List&lt;String&gt; usefulStuff;
  }
  </code></pre>
  <p>My aim is to get a list of all useful things from table C in entity A.</p>
  <p>But I get syntax errors.</p>
  <p>Could you say what's wrong in my example? And maybe you know better way for this purpose?</p>