title: Fetching JPA @OneToMany collection returns empty
body: <p>I am trying out some <a href="https://www.manning.com/books/ejb-3-in-action-second-edition" rel="nofollow">EJB3 in Action</a> examples using Glassfish4 (EclipseLink) + JavaDB. So I have the below relationship</p>
  
  <pre><code>@Entity
  @Table(name = "ITEMS")
  public class Item implements Serializable {
      private static final long serialVersionUID = 1L;
  
      private Long itemId;
      ...
      private List&lt;Bid&gt; bids= new ArrayList&lt;&gt;();
  
      @Id
      @Column(name="ITEM_ID")
      public Long getItemId() {
          return itemId;
      }
  
      public void setItemId(Long itemId) {
          this.itemId = itemId;
      }
  
      @OneToMany(mappedBy="item",fetch=FetchType.EAGER)   
      @JoinColumn(name="BID_ITEM_ID",referencedColumnName="ITEM_ID")
      public List&lt;Bid&gt; getBids() {
          return bids;
      }
  
      public void setBids(List&lt;Bid&gt; bids) {
          this.bids = bids;
      }   
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Entity
  @Table(name="BIDS")
  public class Bid implements Serializable{
      private static final long serialVersionUID = 1L;
  
      ...   
      private Item item;    
      ...
  
      @Id
      @Column(name="BID_ID")
      public Long getBidId() {
          return bidId;
      }
  
      public void setBidId(Long bidId) {
          this.bidId = bidId;
      }
  
      @ManyToOne
      @JoinColumn(name="BID_ITEM_ID",referencedColumnName="ITEM_ID")
      public Item getItem() {
          return item;
      }
  
      public void setItem(Item item) {
          this.item = item;
      }    
      ...        
  }
  </code></pre>
  
  <p>Now when fetching an Item like</p>
  
  <pre><code>@Override
  public List&lt;Bid&gt; getBidsForItem(long itemId) {
      Item item = em.find(Item.class, itemId); // em -&gt; Entity manager
      return item.getBids();
  }
  </code></pre>
  
  <p>the <code>item.getBids()</code> returns an empty list (size = 0, not null). What changes should be done to get Bids for the given Item?</p>
  
  <p><strong>EDIT:</strong><br>
    After enabling query logging as suggested in the comments</p>
  
  <pre><code>&lt;property name="eclipselink.logging.level.sql" value="FINE"/&gt;
  &lt;property name="eclipselink.logging.parameters" value="true"/&gt;
  </code></pre>
  
  <p>I notice that Queries are listed for insert statements but NO query is listed corresponding to <code>em.find(Item.class, itemId)</code>.</p>
  
  <p><strong>EDIT 2 (ANSWER):</strong><br>
  The problem was in my addBids() stateless bean function to which I was passing an Item object. This meant the Item Object is never in persistent context. The right way is to </p>
  
  <ol>
  <li>pass the itemId </li>
  <li>find the Item entity with entity manager find() method. This ensures that the Item object is in persistence context.    </li>
  <li>add bid object to item and item to bid</li>
  <li>call entity manager persist() on Bid.  </li>
  </ol>
  
  <p>Corrected addBids() method:  </p>
  
  <pre><code>public Bid addBids(Date bidDate, Double bidPrice, long itemId, String bidder) {  
          Item item = em.find(Item.class, itemId);  
          Bid bid = new Bid(bidDate, bidPrice, item, bidder);  
          item.getBids().add(bid);  
          em.persist(bid);  
          return bid;  
  }  
  </code></pre>
  
  <p>Thanks to @Chris for pointing out.</p>