title: using composite key with hibernate
body: <p>Suppose i want to have a composite key as street, city for purchase order entity.</p>
  
  <p>Below is how i identify doing it,</p>
  
  <pre><code>    @Embeddable
      public class BillingAddress implements Serializable {
  
          private String street;
          private String city;
  
          public BillingAddress(){
  
          }
  
          public BillingAddress(String street, String city) {
              this.street = street;
              this.city = city;
          }
          //with getters and setters
  }
  
  
  
  @Entity
  @IdClass(BillingAddress.class)
  public class PurchaseOrder {
  
      public PurchaseOrder(BillingAddress billingAddress) {
          street = billingAddress.getStreet();
          city = billingAddress.getCity();
  
      }
  
      @Id
      @AttributeOverrides({
              @AttributeOverride(name = "street", column = @Column(name = "STREET")),
              @AttributeOverride(name = "city", column = @Column(name = "CITY")) })
      private String street;
      private String city;
      private String itemName;
  
      public String getItemName() {
          return itemName;
      }
  
      public void setItemName(String itemName) {
          this.itemName = itemName;
      }
  
  }
  </code></pre>
  
  <p>I want to understand what is really @AttributeOverrides annotation do? Even i change to colomn name to something STREET1 i still see the table created with column name STREET. So what is column = @Column(name = "STREET")) doing here. </p>
  
  <p>Also instead of constructore taking the BillingAddress i can have it like a field of PurchaseOrder class right like,</p>
  
  <pre><code> public class PurchaseOrder {
       BillingAddress billingAddress;
  
  }
  </code></pre>
  
  <p>In this case how this going to change? 
  Do i still need to have private String street; private String city; in PurchaseOrder?</p>
  
  <p>Finally i read that using composite keys should be avoided in new data base system design which using composite primary key is applicable a situation where in order to map the legacy data base tables with out changing the data base table structure right? Is that statement a valid one?</p>
  
  <p>//Edit question</p>
  
  <p>Saving purchase order which billing address is in the field,</p>
  
  <pre><code>PurchaseOrder purchaseOrder = new PurchaseOrder();
  purchaseOrder.setItemName("name");
  
  BillingAddress billingAddress = new BillingAddress();
  billingAddress.setCity("c1"); billingAddress.setStreet("s1");                                                              purchaseOrder.setBillingAddress(billingAddress); 
  session.save(purchaseOrder);
  </code></pre>