title: Foreign key is also part of the primary key
body: <p>Is it possible in plain JPA or JPA+Hibernate extensions to use a foreign key that is also part of the composite primary key?</p>
  
  <pre><code>@TableGenerator(name = "trial", table = "third",
          pkColumnName = "a" , valueColumnName = "b", pkColumnValue = "first")    
  @Entity    
  public class First{    
  @Id
  @GeneratedValue(strategy = GenerationType.TABLE, generator = "trial")
  protected int a;
  
  @OneToMany(mappedBy ="first", cascade = CascadeType.PERSIST)
  @JoinColumn(name = "a")
  protected List&lt;Second&gt; seconds;
  }
  
  class IDC implements Serializable{
   //@Column(name = "a", insertable = false, updatable = false)
      protected int a;
  protected int b;
  }
  
  @Entity    
  @IdClass(IDC.class)
  public class Second{    
      @Id
    //@Column(name = "a", insertable = false, updatable = false)
  protected int a;
  @Id
  protected int b;
  
      @ManyToOne
  @JoinColumn(name = "a"/*, insertable = false, updatable = false*/)
  First first;
  }
  </code></pre>
  
  <p>main:</p>
  
  <pre><code>public class Persister {
  public static void main(String[] args) {
  
  First aFirst = new First();
  
  Second aSecond = new Second();
  aSecond.b = 300;    
  List&lt;Second&gt; scnds = new ArrayList&lt;Second&gt;();
  scnds.add(aSecond);
  
  aFirst.seconds = scnds;
  aSecond.first = aFirst;
  
  
  aEntityManager.getTransaction().begin();
  aEntityManager.persist(aFirst);
  aEntityManager.getTransaction().commit();
  }}
  </code></pre>
  
  <p>The problem is in class "Second":</p>
  
  <p>If I set "insertable = false, updatable = false" at field "a", it throws exception:</p>
  
  <p>"Parameter index out of bounds. 4 is not between valid values of 1 and 3"</p>
  
  <p>And if I set "insertable = false, updatable = false" at the relation of @manyToOne, it run but set 0 in "a" at table "Second"</p>
  
  <p>The Goal: Set the generated value of First.a in Second.a.</p>
  
  <p>/////////////////////////////</p>
  
  <p>the sql to create the DB:</p>
  
  <p>create table first(
  a int,
  primary key (a)
  );</p>
  
  <p>create table second(
  a int,
  b int,
  primary key (a, b)
  );</p>
  
  <p>create table third(
  a varchar(20),
  b int
  );</p>
  
  <p>please ... help</p>
  
  <p>Thanx in advance</p>