title: Find all Foo's containing a Bar with Hibernate
body: <p>I have a class <code>Foo</code> and a class <code>Bar</code> where <code>Foo</code> has a many-to-many relation with <code>Bar</code>.</p>
  
  <p><code>Foo</code> looks like this:</p>
  
  <pre><code>@Entity
  @Table(name = "Foo", catalog = "xxx")
  public class Foo implements java.io.Serializable{
      private int id;
      private String name;
      private Set&lt;Bar&gt; allBars;
  
      public Bar(){
          allBars = new HashSet&lt;Bar&gt;(0);
      }
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "Foo_id", unique = true, nullable = false)
      public int getId(){
          return this.id;
      }
  
      public void setId(int id){
          this.id = id;
      }
  
      @Column(name = "Name", nullable = false)
      public String getName(){
          return this.name;
      }
  
      public void setName(String n) {
          this.name = n;
      }
  
      @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JoinTable (name = "Foo_Bars", catalog = "xxx", joinColumns = {
              @JoinColumn(name = "Foo_id", nullable = false, updatable = false) },
              inverseJoinColumns = { @JoinColumn(name = "Bar_id", nullable = false, updatable = false) })
      public Set&lt;Bar&gt; getAllBars() {
          return this.allBars;
      }
  
      public void setAllBars(Set&lt;Bar&gt; allBars) {
          this.allBars = allBars;
      }
  }
  </code></pre>
  
  <p>And <code>Bar</code> looks like this</p>
  
  <pre><code>@Entity
  @Table (name = "Bar", catalog = "xxx")
  public class Bar implements java.io.Serializable{
      private int    id;
      private String name;
  
      public Bar(){}
  
      @Id
      @GeneratedValue(strategy = IDENTITY)
      @Column(name = "Bar_id", unique = true, nullable = false)
      public int getId() {
          return this.id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      @Column(name="Name",  nullable = false)
      public String getName(){
          return this.name;
      }
  
      public void setName(String nm){
          this.name = nm;
      }
  }
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>I want to have a method called <code>public List&lt;Foo&gt; retrieveFooForBarId(int bar_id)</code> in my <code>FooDaoImpl</code> class so I can retrieve all <code>Foo</code>'s that contain a certain <code>Bar</code>.</p>
  
  <p><em>Example</em></p>
  
  <p>I have 2 <code>Foo</code>'s, the first contains 2 <code>Bar</code>'s, the <code>Bar</code>'s with <code>id=1</code> and <code>id=2</code>. The second <code>Foo</code> contains also contains 2 <code>Bar</code>'s, one with <code>id=1</code> and one with <code>id=3</code>. Both <code>Foo</code>'s contain the <code>Bar</code> with <code>id=1</code>.</p>
  
  <p>In <code>SQL</code> the query would be</p>
  
  <pre><code>SELECT * FROM Foo
  LEFT JOIN Foo_Bars ON Foo.Foo_id = Foo_Bars.Foo_id
  WHERE Foo_Bars.Bar_id = 1;
  </code></pre>
  
  <p>How do I achive this with Hibernate?</p>