title: Hibernate OneToMany using annotations with legacy database
body: <p>I'm working with a legacy database that isn't normalized at all. They've been coding relationships directly in the program (written in Uniface). Now, they are migrating to Java. Sadly, I can't change the database, but I need to map a OneToMany relationship between tables.</p>
  
  <p>For instance, I have a table Invoice and a table Item.</p>
  
  <p>Table <strong>Invoice</strong> has these keys. The first three of them are used as foreign keys for the relationship with Item:  </p>
  
  <pre><code>invoice_serial_id,  
  invoice_number_id,  
  invoice_date,
  invoice_otherkey1,
  invoice_otherkey2  
  </code></pre>
  
  <p>Table <strong>Item</strong> has these keys, the first three have the same type and value than their corresponding Invoice keys.</p>
  
  <pre><code>item_serial_id,
  item_number_id,
  item_date,
  item_otherkey1
  </code></pre>
  
  <p>I have created the tables using reverse engineering from hibernate, which generated Invoice.java, InvoiceId.java, Item.java and ItemId.java, then manually created @OneToMany and @ManyToOne annotations. Here's the code so far:</p>
  
  <p><strong>Invoice.java</strong>  </p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Table(name = "INVOICE")
  public class Invoice implements java.io.Serializable {
  
      private static final long serialVersionUID = -2073462863929322522L;
  
      @EmbeddedId
          @AttributeOverrides( {
              @AttributeOverride(name = "invoiceSerialId", column = @Column(name = "INVOICE_SERIAL_ID", nullable = false, length = 9)),
              @AttributeOverride(name = "invoiceNumberId", column = @Column(name = "INVOICE_NUMBER_ID", nullable = false, length = 9)),
              @AttributeOverride(name = "invoiceDate", column = @Column(name = "INVOICE_DATE", nullable = false, length = 7)),
              @AttributeOverride(name = "invoiceOtherkey1", column = @Column(name = "INVOICE_OTHERKEY1", nullable = false, length = 7)),
              @AttributeOverride(name = "invoiceOtherkey2", column = @Column(name = "INVOICE_OTHERKEY2", nullable = false, length = 7))
      })
      private InvoiceId id;
  
      @OneToMany(mappedBy = "invoice")
      private Set&lt;Item&gt; items;
  }
  </code></pre>
  
  <p><strong>Item.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Table(name = "ITEM")
  public class Item implements java.io.Serializable {
      private static final long serialVersionUID = -2840038244580784867L;
  
      @EmbeddedId
          @AttributeOverrides( {
              @AttributeOverride(name = "itemSerialId", column = @Column(name = "ITEM_SERIAL_ID", nullable = false, length = 9)),
              @AttributeOverride(name = "itemNumberId", column = @Column(name = "ITEM_NUMBER_ID", nullable = false, length = 9)),
              @AttributeOverride(name = "itemDate", column = @Column(name = "ITEM_DATE", nullable = false, length = 7)),
              @AttributeOverride(name = "itemOtherkey1", column = @Column(name = "ITEM_OTHERKEY1", nullable = false, length = 7))
      })
      private ItemId id;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumns({
          @JoinColumn(name = "ITEM_SERIAL_ID", referencedColumnName = "INVOICE_SERIAL_ID"),
          @JoinColumn(name = "ITEM_NUMBER_ID", referencedColumnName = "INVOICE_NUMBER_ID"),
          @JoinColumn(name = "ITEM_DATE", referencedColumnName = "INVOICE_DATE")
      })
      private Invoice invoice;
  }
  </code></pre>
  
  <p>I get the following error:  </p>
  
  <pre class="lang-java prettyprint-override"><code>org.hibernate.AnnotationException: referencedColumnNames(INVOICE_SERIAL_ID, INVOICE_NUMBER_ID, INVOICE_DATE) of com.just.an.stupid.example.app.Item.invoice referencing com.just.an.stupid.example.app.Invoice not mapped to a single property
  </code></pre>
  
  <p>I've also tried following the steps <a href="https://stackoverflow.com/questions/1483026/hibernate-foreign-key-has-the-wrong-number-of-columns">here</a>, and create a @joinTable on Invoice such as  </p>
  
  <pre class="lang-java prettyprint-override"><code>@JoinTable(name = "INVOICE_ITEM",
      joinColumns = {
          @JoinColumn(name = "ITEM_SERIAL_ID", referencedColumnName = "INVOICE_SERIAL_ID"),
          @JoinColumn(name = "ITEM_NUMBER_ID", referencedColumnName = "INVOICE_NUMBER_ID"),
          @JoinColumn(name = "ITEM_DATE", referencedColumnName = "INVOICE_DATE")
      },
      inverseJoinColumns = {
          @JoinColumn(name = "INVOICE_SERIAL_ID", referencedColumnName = "ITEM_SERIAL_ID"),
          @JoinColumn(name = "INVOICE_NUMBER_ID", referencedColumnName = "ITEM_NUMBER_ID"),
          @JoinColumn(name = "INVOICE_DATE", referencedColumnName = "ITEM_DATE")
      }
  )
  </code></pre>
  
  <p>but I got this:  </p>
  
  <pre class="lang-java prettyprint-override"><code>org.hibernate.AnnotationException: A Foreign key refering com.just.an.stupid.example.app.Invoice from com.just.an.stupid.example.app.Item has the wrong number of column. should be 5
  </code></pre>
  
  <p>How can I create a relationship using this database? Or should I give up and manually select Items?</p>