title: @ManyToOne(updatable=false) - how it should work?
body: <p>I want to have a <strong>read-only</strong> functionality in one of my entities. I know that in JPA 2.0 we don't have such functionality per se. I thought we can achieve it using <code>updateable=false, insertable=false</code> but I don't think I get how it works.</p>
  
  <p>Assume that I have two entities: <code>OrderedItem</code> and <code>Customer</code>:</p>
  
  <pre><code>@Entity
  public class OrderedItem {
  
      @Id
      @GeneratedValue
      private int id;
  
      private String name;
  
      @ManyToOne
      @JoinColumn(updatable = false)
      private Customer owner;
  
      // bunch of simple getters and setters
  }
  
  @Entity
  public class Customer {
  
      @Id
      @GeneratedValue
      private int id;
  
      private String name;
  
      @OneToMany(mappedBy="owner")
      private Set&lt;OrderedItem&gt; orderedItems;
  
      // bunch of simple getters and setters
  }
  </code></pre>
  
  <p>Now consider the following code:</p>
  
  <pre><code>Customer john = new Customer();
  john.setName("John");
  
  OrderedItem milk = new OrderedItem();
  milk.setName("Milk");
  milk.setOwner(john);
  
  Set&lt;OrderedItem&gt; items = new HashSet&lt;OrderedItem&gt;();
  items.add(milk);        
  john.setOrderedItems(items);
  
  // This starts the EM transaction
  startTx();
  
  em.persist(john);
  em.persist(milk);
  
  stopTx();
  
  startTx();
  
  OrderedItem milkFromPC = em.find(OrderedItem.class, milk.getId());
  
  System.out.println(milkFromPC.getName() + " ordered by customer: " + 
                     milkFromPC.getOwner().getName());
  
  // Changing the state of Owner entity through the OrderedItem
  milkFromPC.getOwner().setName("Terrence");
  
  stopTx();
  </code></pre>
  
  <p>Now, without <code>@JoinColumn(updatable = false)</code> in <code>OrderedItem</code> entity, the <code>OrderedItem</code> would be fetched from the PC, I'd access it's owner - a <code>Customer</code> - and would successfully modified its name. It wouldn't be a surprise because the <code>Customer</code> was also in managed state, so it had to be reflected in the database.</p>
  
  <p><strong>However</strong>, I assumed that <code>updateable=false</code> in <code>@JoinColumn</code> set on the One side of the relationship would prevent the UPDATE SQL statement from occurring. Unfortunately in the end <strong>I can see the name changed in the database</strong> (it's "Terrence" instead of "John"). I can also see the executed SQL UPDATE query:</p>
  
  <blockquote>
    <p>[EL Fine]: 2011-11-30
    23:41:27.941--ClientSession(16862753)--Connection(11024915)--Thread(Thread[main,5,main])--UPDATE
    CUSTOMER SET NAME = ? WHERE (ID = ?)  bind => [Terrence, 1]</p>
  </blockquote>
  
  <p>So - what does this <code>updateable=false</code> really do? Why do I need it? Does it protect only my foreign key from being changed? Is it like 'you can't change the entity but you can change the state of the entity'?</p>