title: @OneToMany and composite primary keys?
body: <p>I'm using Hibernate with annotations (in spring), and I have an object which has an ordered, many-to-one relationship which a child object which has a composite primary key, one component of which is a foreign key back to the id of the parent object.</p>
  
  <p>The structure looks something like this:</p>
  
  <pre><code>+=============+                 +================+
  | ParentObj   |                 | ObjectChild    |
  +-------------+ 1          0..* +----------------+
  | id (pk)     |-----------------| parentId       |
  | ...         |                 | name           |
  +=============+                 | pos            |
                                  | ...            |
                                  +================+
  </code></pre>
  
  <p>I've tried a variety of combinations of annotations, none of which seem to work.  This is the closest I've been able to come up with:</p>
  
  <pre><code>@Entity
  public class ParentObject {
      @Column(nullable=false, updatable=false)
      @Id @GeneratedValue(generator="...")
      private String id;
  
      @OneToMany(mappedBy="parent", fetch=FetchType.EAGER, cascade={CascadeType.ALL})
      @IndexColumn(name = "pos", base=0)
      private List&lt;ObjectChild&gt; attrs;
  
      ...
  }
  
  @Entity
  public class ChildObject {
      @Embeddable
      public static class Pk implements Serializable {
          @Column(nullable=false, updatable=false)
          private String parentId;
  
          @Column(nullable=false, updatable=false)
          private String name;
  
          @Column(nullable=false, updatable=false)
          private int pos;
  
          @Override
          public String toString() {
              return new Formatter().format("%s.%s[%d]", parentId, name, pos).toString();
          }
  
          ...
      }
  
      @EmbeddedId
      private Pk pk;
  
      @ManyToOne
      @JoinColumn(name="parentId")
      private ParentObject parent;
  
      ...
  }
  </code></pre>
  
  <p>I arrived at this after a long bout of experimentation in which most of my other attempts yielded entities which hibernate couldn't even load for various reasons.</p>
  
  <p>UPDATE: Thanks all for the comments; I have made some progress.  I've made a few tweaks and I think it's closer (I've updated the code above).  Now, however, the issue is on insert.  The parent object seems to save fine, but the child objects are not saving, and what I've been able to determine is that hibernate is not filling out the parentId part of the (composite) primary key of the child objects, so I'm getting a not-unique error:</p>
  
  <pre><code>org.hibernate.NonUniqueObjectException:
     a different object with the same identifier value was already associated 
     with the session: [org.kpruden.ObjectChild#null.attr1[0]]
  </code></pre>
  
  <p>I'm populating the <code>name</code> and <code>pos</code> attributes in my own code, but of course I don't know the parent ID, because it hasn't been saved yet.  Any ideas on how to convince hibernate to fill this out?</p>
  
  <p>Thanks!</p>