title: DDD aggregate IDs and foreign keys
body: <p>I am fairly new to DDD.  I've seen a number of example projects that use a generic AggregateId (with a String containing a GUID) as a key for an aggregate root.  </p>
  
  <p>I was wondering how to refer to the aggregateRoot from a child in a OneToMany relationship.</p>
  
  <p>Say there is a Order AggregateRoot and a OrderLine.  Would it be wise to have an extra generated (eg. sequence) id, next to the GUID, so the OrderLine can refer to that on database level?  Or is the foreign key of orderLine to order the GUID ?  Are there performance implications?</p>
  
  <p>eg.</p>
  
  <p>BaseAggregateRoot:</p>
  
  <pre><code>@MappedSuperclass  
  public abstract class BaseAggregateRoot {
      @EmbeddedId
      @AttributeOverrides({
         @AttributeOverride
         (name = "idValue", column = @Column(name = "aggregateId", nullable = false))
      })
      protected AggregateId aggregateId;
      ...
  </code></pre>
  
  <p>Order:</p>
  
  <pre><code>@Entity
  public class Order extends BaseAggregateRoot{
      // is this ID necessary?
      @Id
      @GeneratedValue(generator = "OrderSequenceGenerator")
      @SequenceGenerator(name = "OrderSequenceGenerator", sequenceName = "ORD_SEQ1", allocationSize = 1)
      @Column(name = "ord_seq")
      private Long id;
  
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      @JoinColumn(name = "aggregateId")  &lt;-- should this point to ID or aggregateId?
      private List&lt;OrderLine&gt; orderLines;
  </code></pre>