title: Spring Template Project jpa annotations
body: <p>I have a one to many relationship: Order and Items. An Order may have lots of items
  The code below (<a href="http://devhub.fm/spring-3-0-hibernate-template-utility-project-part-1-2/" rel="nofollow">all code</a>) is auto generated by Spring Template project:</p>
  
  <p>On the Order Side:</p>
  
  <pre><code>@OneToMany(cascade=CascadeType.ALL)
  @JoinColumn(name="ORDER_ID")
  private Collection&lt;Item&gt; items = new LinkedHashSet&lt;Item&gt;();
  </code></pre>
  
  <p>On the Item Side:</p>
  
  <pre><code>@ManyToOne
  private Order order;
  </code></pre>
  
  <p>Test code that works:</p>
  
  <pre><code>order.getItems().add(new Item());
  session.save(order)
  Order other = (Order) session.get(Order.class, order.getId());
  assertEquals(1, other.getItems().size());
  </code></pre>
  
  <p>While searching through the internet and various sites with tutorials on 1 &lt;---> Many relationship I usually find code such as example: <a href="http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/" rel="nofollow">hibernate one to many</a> </p>
  
  <p>The One Side (Stock)</p>
  
  <pre><code>@OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
  public Set&lt;StockDailyRecord&gt; getStockDailyRecords() {
  return this.stockDailyRecords;}
  </code></pre>
  
  <p>The many Side (Stock Record)</p>
  
  <pre><code> @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "STOCK_ID", nullable = false)
   public Stock getStock() {
   return this.stock;}
  </code></pre>
  
  <ul>
  <li>Why is the JoinColumn Annotation placed differently?The owner of the relationship according to spring code  is on the side of the One and not the many. why is that?</li>
  <li>Althouth the relationship does not have the mappedBy atttibute at all so one could say that is one direction from order to items it seems bidirectional. i.e order.getItems().add(new Item)  and order.getItems().iterator().next().getOrder() works as well!! On top of that the tables created in the actual database seem ok.</li>
  <li>If we refactor the first code (see below the refactored) and place the annotations as the tutorials suggest for bidirectional mapping then  2/3 of the tests fail because no items are saved in the database (Assertion error expected 1 but was 0 when using order.getItems().add(new Item())) .Note that the spring tempate test code is left unchanged.Why?</li>
  <li>Does it have to do with the annotations being placed on get methods vs annotations placed on private fields? </li>
  <li>What is the proper way of saving Items and Order in database (java code)?</li>
  </ul>
  
  <p>Thanks</p>
  
  <p>The code refactored that fails the tests:</p>
  
  <p>The One Side:</p>
  
  <pre><code>@OneToMany(cascade=CascadeType.ALL,mappedBy="order")
  private Collection&lt;Item&gt; items = new LinkedHashSet&lt;Item&gt;();
  </code></pre>
  
  <p>The many side:   </p>
  
  <pre><code>@ManyToOne
  @JoinColumn(name="ORDER_ID")
  private Order order;
  </code></pre>