title: OneToMany relationship using non-primary composite key
body: <p>I have a table structure like this</p>
  <p>Good Assignments Entity</p>
  <pre><code>@Embeddable
  public class GoodAssignmentId {
    String clientId,
    String assignmentNumber;
    LocalDate effectiveDate;
  
    // Getters and setters
  }
  
  @Entity
  @IdClass(GoodAssignmentId.class)
  class GoodAssignment {
    @id
    String clientId;
  
    @Id
    String assignmentNumber;
    
    @Id
    LocalDate effectiveDate;
  
    @OneToMany(mappedBy = &quot;parentKey&quot;)
    Set&lt;GoodTasks&gt; children;
    
    String description;
  
    // getters and setters goes below      
  }
  </code></pre>
  <p>Bad Assignments Entity</p>
  <pre><code>@Entity
  @IdClass(BadAssignmentId.class)
  class BadAssignment {
    @id
    String clientId;
  
    @Id
    String assignementNumber;
    
    @Id
    LocalDate effectiveDate;
  
    String description;
  
    // Getters and setters goes below
  }
  </code></pre>
  <p>Child entities</p>
  <pre><code>@Entity
  @IdClass(ParentTasksId.class)
  @DiscriminatorColumn(name = &quot;fieldD&quot;, discriminatorType = DiscriminatorType.STRING)
  class ParentTasks {
    @Id
    String clientId;
  
    @Id
    String assignmentNumber;
  
    @Id
    String taskNumber;
  }
  
  @Entity
  @DiscriminatorValue(&quot;G&quot;)
  class GoodTasks extends ParentTasks {
     @ManyToOne
     @JoinColumns({
       @JoinColumn(name = &quot;clientId&quot;, referencedColumName = &quot;clientId&quot;),
       @JoinColumn(name = &quot;assignmentNumber&quot;, referencedColumName = &quot;assignmentNumber&quot;)
     })
     GoodAssignments parentKey;
     
     other fields....
  }
  </code></pre>
  <p>This shows the error <code>referencedColumnNames(fieldA, fieldB) of .... not mapped to a single property.</code></p>
  <p>Unfortunately I cannot change the table structure. <code>TableA</code> has 3 columns as primary key, but only two of them forms the primary key in table B along with another field (<code>fieldD</code> via <code>@DiscriminatorValue</code> used by multiple classes). How can I map this relationship to get list of <code>TableB</code> items in <code>TableA</code>?</p>
  <p>Example Class Diagram:
  Effective date in the assignments tables is not part of tasks. So this is not a perfect relationship in JPA terms. It's a legacy design which cannot be changed for some reasons.</p>
  <p><a href="https://i.stack.imgur.com/G29om.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G29om.png" alt="enter image description here" /></a></p>