title: Mapping a read-only database with a many-to-many relation without a join table
body: <p>I have a question similar to <a href="https://stackoverflow.com/q/5409560/387981">@ManyToMany without join table (legacy database)</a> with an additional issue.</p>
  
  <p>I have two tables <code>A</code> and <code>B</code></p>
  
  <ul>
  <li><code>A</code> with a multiple column primary key (<code>ID</code> and <code>ID2</code>)</li>
  <li><code>B</code> with a multiple column primary key (<code>ID</code> and <code>ID3</code>)</li>
  </ul>
  
  <p>An row in <code>A</code> can reference several rows in <code>B</code> (<code>B.ID = A.ID</code>) and a row in <code>B</code> can be referenced by several rows in <code>A</code>.</p>
  
  <p><strong>EDIT</strong>: the database is a read-only legacy database that I cannot change. I do not need to map the relationships with JPA (I could just do it in my program logic with additional selects) but it would be nice.</p>
  
  <p>It is basically a many-to-many relationship without a join table. Since, as for the linked question, I just have to read the tables, I tried with two one-to-many relationships in both classes.</p>
  
  <p>The additional problem that I have is that both <code>ID</code>s used for the join are not the primary key.</p>
  
  <p>I have the following classes:</p>
  
  <pre><code>@Entity
  @Table( name = "A" )
  @IdClass( PrimaryKeysA.class )
  public class A {
  
      @Id
      @Column( name = "ID", insertable = false, updatable = false, columnDefinition = "char" )
      private String id;
  
      @Id
      @Column( name = "ID2", insertable = false, updatable = false )
      private int id2;
  
      @OneToMany( cascade = CascadeType.ALL )
      @JoinColumn( name = "ID", columnDefinition = "char", referencedColumnName = "ID" )
      private Set&lt; B &gt; setOfBs;
  
  }
  
  @Entity
  @Table( name = "B" )
  @IdClass( PrimaryKeysB.class )
  public class B {
  
      @Id
      @Column( name = "ID", insertable = false, updatable = false, columnDefinition = "char" )
      private String id;
  
      @Id
      @Column( name = "ID3", insertable = false, updatable = false )
      private int id3;
  
      @OneToMany( cascade = CascadeType.ALL )
      @JoinColumn( name = "ID", columnDefinition = "char", referencedColumnName = "ID" )
      private Set&lt; A &gt; setOfAs;
  
  }
  </code></pre>
  
  <p>Hibernate generates the following error:</p>
  
  <pre><code>Exception while preparing the app : referencedColumnNames(ID) of package.B referencing package.A not mapped to a single property
  </code></pre>
  
  <p>I don't really get the message: <code>B.id</code> is referencing a single property in <code>A</code> (<code>A.id</code>).</p>
  
  <p>EDIT: as requested:</p>
  
  <pre><code>public class PrimaryKeysA implements Serializable {
  
  private static final long   serialVersionUID    = 1L;
  
  private int    id1;
  private int    id2;
  
      // getters/setters/equals/hashcode
  
  }
  </code></pre>
  
  <p><code>PrimaryKeysB</code> is similar with id3 instead of id2. Both classes <code>A</code> and <code>B</code> are simplified (anonymized) examples.</p>