title: org.hibernate.AnnotationException: referencedColumnNames(P0, P1, P2)... not mapped to a single property
body: <p>I am using MS SQL server, Spring 3,  Hibernate JPA 2.0. 
  I have four tables Level0, Level1, level2 and Level3. 
  Level0 has an id P0 as a primary key. Level1 has a compsoite primary key with P0 from level0 as FK and P1. Level2 has a composite key Level0.P0, Level1.P1 and P2. And so on.
  The table definitions are given below.</p>
  
  <pre><code>CREATE TABLE Level0
  (
     P0 integer NOT NULL,
     Level0_name varchar(50) NOT NULL,
     PRIMARY KEY (P0)
  );
  
  CREATE TABLE Level1
  (
     P0 integer NOT NULL,
     P1 varchar(10) NOT NULL,
     Level1_Name varchar(60),
     PRIMARY KEY (P0, P1)
  );
  ALTER TABLE Level1 ADD CONSTRAINT FK_Level1 FOREIGN KEY (P0) REFERENCES Level0(P0);
  
  CREATE TABLE Level2
  (
     P0 integer NOT NULL,
     P1 varchar(10) NOT NULL,
     P2 varchar(10) NOT NULL,
     Level2_Name varchar(20),
     PRIMARY KEY (P0, P1, P2)
  
  );
  ALTER TABLE Level2 ADD CONSTRAINT FK_Level2 
  FOREIGN KEY (P0, P1) REFERENCES Level1(P0, P1);
  
  CREATE TABLE Level3
  (
     P0 integer NOT NULL,
     P1 varchar(10) NOT NULL,
     P2 varchar(10) NOT NULL,
     P3 varchar(10) NOT NULL,
     Level3_Name varchar(20) NOT NULL,
     PRIMARY KEY (P0, P1, P2, P3)
  );
  ALTER TABLE Level3 ADD CONSTRAINT FK_Level3 
  FOREIGN KEY (P0, P1, P2) REFERENCES Level2(P0, P1, P2);
  </code></pre>
  
  <p>I have corresponding Java classes for each of the tables. So Level0 has a collection of Level1s, Level1 has collection of Level2s and so on.
  Level3 contains Level2 object, Level2 contains Level1 object and so on. Here is the Java classes for Level1 and Level2.</p>
  
  <pre><code>@Entity
  @Table(name="Level1")
  public class Level1 extends DomainObject
  {
     private String p1;
     private Level0 level0;
     private String name;
     private List&lt;Level2&gt; level2s = new ArrayList&lt;Level2&gt;();
  
     @Id
     @NotNull
     @Column(name="P1")
     public String getP1()
     {
        return(p1);
     }
  
     @NotNull
     @ManyToOne(cascade={CascadeType.DETACH,
                 CascadeType.MERGE,
                 CascadeType.PERSIST,
                 CascadeType.REFRESH})
     @JoinColumn(name="P0",
             referencedColumnName="P0")
     public Level0 getLevel0()
     {
        return(level0);
     }
  
  }
  ----------------------------------------------------------
  @Entity
  @Table(name="Level2")
  public class Level2 extends DomainObject
  {
     private String p2;
     private Level1 level1;
     private String name;
     private List&lt;Level3&gt; level3s = new ArrayList&lt;Level3&gt;();
  
     @Id
     @NotNull
     @Column(name="P2")
     public String getP2()
     { return(p2);
     }
  
     @ManyToOne(fetch=FetchType.LAZY)
     @JoinColumns({
         @JoinColumn(name="P0", referencedColumnName="P0"),
         @JoinColumn(name="P1", referencedColumnName="P1")
     })
     public Level1 getLevel1()
     { return(level1);
     }
  
     @NotNull
     @Column(name="Level2_Name")   
     public String getName()
     { return(name);
     }
  
     @OneToMany(orphanRemoval=true,
       cascade=CascadeType.ALL,
       mappedBy="level2",
       fetch = FetchType.LAZY)
     public List&lt;Level3&gt; getLevel3s()
     {
      return(level3s);
     }
  }
  </code></pre>
  
  <p>These relationships between Level0, Level1 and Level2 work great. The code works correctly.
  When I add the Level3 class, it throws errors. Here is how Level3 looks like.</p>
  
  <pre><code>public class Level3 extends DomainObject
  {
     private String p3;
     private Level2 level2;
     private String name;
  
  
     @Id
     @NotNull
     @Column(name="P3")
     public String getP3()
     {
        return(p3);
     }
  
     @ManyToOne(fetch=FetchType.LAZY)
     @JoinColumns({
         @JoinColumn(name="P0", referencedColumnName="P0"),
         @JoinColumn(name="P1", referencedColumnName="P1"),
         @JoinColumn(name="P2", referencedColumnName="P2")
     })
     public Level2 getLevel2()
     {
        return(level2);
     }
  
     @NotNull
     @Column(name="Level3_Name")   
     public String getName()
     {
        return(name);
     }
  
  }
  </code></pre>
  
  <p>I get the following error:
  <b>Caused by: org.hibernate.AnnotationException: referencedColumnNames(P0, P1, P2) of 
  Level3.level2 referencing Level2 not mapped to a single property.</b></p>
  
  <p>Level2 has 2 Foreign keys and its own field, the 3 making a composite primary key. And that works. But Level3 has 3 foreign keys. And that does not.</p>
  
  <p>Can someone help me with this? Thanks.</p>