title: JPA entity with composite primary and foreign keys
body: <p>I have an entity with a composite primary key consisting of two fields, one of which is also part of a composite foreign key.</p>
  
  <p>Background: I have entities <code>Person</code>,<code>Area</code>, and <code>Session</code>.</p>
  
  <p><code>Person</code> has many-to-many relationships with <code>Area</code> and <code>Session</code>, using join entities called <code>PersonArea</code> and <code>PersonSession</code>. </p>
  
  <p>So, I have <code>PersonSession</code>, with primary key of (<code>personId</code>, <code>sessionId</code>). 
  <code>PersonId</code> and <code>SessionId</code> are themselves foreign keys to <code>Person</code> and <code>Session</code>.</p>
  
  <p><code>PersonSession</code> also has a field <code>areaId</code>.
  I want (<code>personId</code>, <code>areaId</code>) to be a composite foreign key to <code>PersonArea</code>.</p>
  
  <p>My code for PersonSession:</p>
  
  <pre><code>@Entity
  @Table(name="person_session")
  @IdClass(PersonSession.ID.class)
  public class PersonSession {
  
    @Id
    private int personId ;
  
    @Id
    private int sessionId;
  
    @ManyToOne
    @JoinColumn(name = "personId", updatable = false, insertable = false, referencedColumnName = "id")
    private Person person;
  
    @ManyToOne
    @JoinColumn(name = "sessionId", updatable = false, insertable = false, referencedColumnName = "id")
    private Session session;
  
    @ManyToOne//(cascade = CascadeType.ALL)
    @JoinColumns({
      @JoinColumn(name = "personId", updatable = false, insertable = false),
      @JoinColumn(name = "areaId", updatable = false, insertable = false),
    })
    private PersonArea personArea;
  </code></pre>
  
  <p>}</p>
  
  <p>Code for <code>PersonSession.Id</code></p>
  
  <pre><code>public static class ID implements Serializable {
      private int personId;
      private int sessionId;
  }
  </code></pre>
  
  <p>This seems OK, it creates all the correct relationships in the database. The problem comes when I try to insert PersonSession objects - the areaId column is always null, I think that is because it's defined a <code>updatable=false, insertable=false</code>.</p>
  
  <p>However, if I try and make it updatable and insertable, I get an exception complaining the personId is a repeated column:</p>
  
  <pre><code>Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: foo.bar.PersonSession column: personId (should be mapped with insert="false" update="false")
  </code></pre>
  
  <p>How can I have the required relationships AND have areaId updatable and insertable?</p>