title: One to Many Mapping to JPA Object with Composite Key
body: <p>I am trying to figure out how to map an entity with a composite key and has an attribute that is part of a composite key on the joining table.  </p>
  
  <p>I have SalesOrderNumber on the parent table and 3 attributes on the child table, one of which is SalesOrderNumber.</p>
  
  <p>For the main table (SalesHeader) I am trying to get the SalesOrder entity using the SalesOrderNumber as the joining column so that when I retrieve the SalesHeader, I have a SalesOrder object as well.</p>
  
  <p>Thanks in advance.</p>
  
  <h1>Database</h1>
  
  <h3>SalesHeader Table</h3>
  
  <pre><code>ClientKey - PK, int
  InvoiceNumber - PK, String
  InvoiceBillingDateKey - PK, int
  SalesOrderNumber - String, not null
  </code></pre>
  
  <h3>SalesOrder Table</h3>
  
  <pre><code>ClientKey - PK, int
  SalesOrderNumber - PK, String
  SalesOrderCreatedOnDateKey - PK, int
  </code></pre>
  
  <h1>Mappings</h1>
  
  <h3>SalesHeader JPA Object</h3>
  
  <pre><code>@EmbeddedId
      private FactSalesHeaderKey  factSalesHeaderKey;
  
  @OneToOne(fetch=FetchType.EAGER)
  
  @JoinColumn(name = "SalesOrderNumber", referencedColumnName="SalesOrderNumber", nullable=false)
  
  private FactSalesOrder factSalesOrder;  &lt;==== TRYING TO SET THIS
  
  public FactSalesOrder getFactSalesOrder() {
      return factSalesOrder;
  }
  
  public void setFactSalesOrder(FactSalesOrder factSalesOrder) {
      this.factSalesOrder = factSalesOrder;
  }
  </code></pre>
  
  <h3>SalesOrderKey - Composite PK Object</h3>
  
  <pre><code>@Embeddable
  
      public class FactSalesOrderKey implements Serializable{
  
          private static final long serialVersionUID = 1L;
          @Column(name="ClientKey")
          private Long  clientKey;
          @Column(name="SalesOrderNumber")
          private String salesOrderNumber;
          @Column(name="SalesOrderCreatedOnDateKey")
          private Long salesOrderCreatedOnDateKey;
      }
  </code></pre>
  
  <h3>SalesOrder - JPA Object</h3>
  
  <pre><code>@Entity
  
      public class FactSalesOrder {
  
      @EmbeddedId
      private FactSalesOrderKey factSalesOrderKey;
      @Column(name="OverallBillingStatus", nullable = true)
      private Long overallBillingStatus;
      @Column(name="OverallDeliveryStatus", nullable = true)
      private Long overallDeliveryStatus;
      @Column(name="OverallOrderStatus", nullable = true)
      private Long overallOrderStatus;
  
  }
  </code></pre>