title: join on foreign key with hibernate
body: <p>i am a hibernate-beginner and have a problem when trying to join 2 tables with hibernate. What i am trying to do is get the list of products a certain store has depending on the store id, but what i am getting is the list of all available products in the database listed under every store.</p>
  
  <p>Here's the code for <code>Product.java</code> :</p>
  
  <pre><code>@Entity
  @Table (name = "products")
  public class Product implements Serializable{
  
  /**
   * 
   */
  private static final long serialVersionUID = -1001086120280322279L;
  
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column (name = "product_id")
  private int product_id;
  
  @Column(name = "product_name", unique=true)
  private String product_name;
  
  @JoinColumn(name = "store", referencedColumnName="store_id")
  @ManyToOne(cascade=CascadeType.ALL)
  private Store store;
  </code></pre>
  
  <p>etc..</p>
  
  <p>and here's the code for <code>Store.java</code> :</p>
  
  <pre><code>@Entity
  @Table(name = "stores")
  public class Store implements Serializable{
  /**
   * 
   */
  private static final long serialVersionUID = 4497252090404342019L;
  
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column (name = "store_id")
  private int store_id;
  
  @Column(name = "store_name", unique=true)
  private String store_name;
  
  @JoinColumn(name="store", referencedColumnName= "store_id")
  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
  private List&lt;Product&gt; productList;
  </code></pre>
  
  <p>etc..</p>
  
  <p>Here's the output: (products A should be under Butik A and products B under Butik B)</p>
  
  <pre><code>Butik: Butik A
  Produkt: Banana A
  Produkt: Morot A
  Produkt: Banana B
  Produkt: Apple B
  
  Butik: Butik B
  Produkt: Banana A
  Produkt: Morot A
  Produkt: Banana B
  Produkt: Spple B
  </code></pre>
  
  <p>I have 2 additional classes, ProductDAO and StoreDAO that take care of the query, the code is similar in both classes except the table-name/class-name. </p>
  
  <pre><code>public class ProductDAO { 
     public static List&lt;Product&gt; getStoreProductsList() { 
       Session hibernateSession = HibernateUtil.getSession(); 
       hibernateSession.beginTransaction();    
       Query query = hibernateSession.createQuery("from Product");
       hibernateSession.getTransaction().commit();     
       List&lt;Product&gt; storeProducts = query.list(); 
       return storeProducts;   
     }
  }
  </code></pre>
  
  <p>Is there any way of solving this with hibernate only?</p>
  
  <p>Thanks</p>