title: Hibernate - using composite key that contains a parent id - OneToMany
body: <p><em>(The original question has changed, see updates below.)</em></p>
  
  <p>I have trouble letting hibernate set the primary key of a child object. The primary key is identical with the parent's primary key.</p>
  
  <p>This is what the parent looks like:</p>
  
  <pre><code>@Entity
  @Table(name = "PARENT")
  @SequenceGenerator(name = "SEQ", allocationSize = 1, sequenceName = "seq_parent_id")
  public class Parent {
  
      @Id
      @Column(name = "ID")
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
      private Long id;
  
      @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @Fetch(value = FetchMode.SUBSELECT)
      @JoinColumn(name = "PARENT_ID")
      private Set&lt;Child&gt; children;
  </code></pre>
  
  <p>This is what the child looks like:</p>
  
  <pre><code>@Entity
  @Table(name = "CHILDREN")
  public class Child {
  
      @Id
      @Column(name = "PARENT_ID")
      private Long parentId;
  </code></pre>
  
  <p>Now, when I run a JUnit repository test where I am trying to save a parent entity with a child entity, I get the exception:</p>
  
  <pre><code>org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): ...
  </code></pre>
  
  <p>What is the correct way of letting hibernate automatically set the Child.parentId to parent.id ?</p>
  
  <p>I tried giving Child a Parent. But ran into other exceptions as Parent is not an Id.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I realised that the Child's Id is not a real primary key. It is just a mapping key. There can be several child entries for a parent.id.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Now I start realising what I really need:</p>
  
  <p>The child's primary key is a composite key out of three values: the parent id and two more String values.</p>
  
  <pre><code>@Entity
  @Table(name = "CHILDREN")
  public class Child {
  
      @Column(name = "PARENT_ID")
      private Long parentId;
  
      @Column(name = "PROPERTY_NAME")
      private String propertyName;
  
      @Column(name = "ANOTHER_PROPERTY")
      private String anotherProperty;
  
      @Column(name = "PROPERTY_VALUE")
      private String propertyValue;
  </code></pre>
  
  <p>So I need to map a composite key using parentId, properyName and anotherProperty. propertyValue is the actual value of that entry.</p>
  
  <p>I think I might need to use IdClass or embeddedId. I tried, but without success so far.</p>