title: org.hibernate.MappingException: Foreign key XXX must have same number of columns as the referenced primary key YYY
body: <p>Having a following SQL table:</p>
  
  <pre><code>create table users_posts_ratings_map (
    postId integer not null references posts (id),
    userId integer not null references users (id),
    ratingId integer not null references ratings (id),
    primary key (postId, userId)
  );
  </code></pre>
  
  <p>and Following JPA-Annotated POJOs:</p>
  
  <p>RatingId.java:</p>
  
  <pre><code>@Embeddable
  public class RatingId implements Serializable {
      @ManyToOne
      @JoinColumn(name = "userId")
      private User user;
  
      @ManyToOne
      @JoinColumn(name = "postId")
      private Post post;
  
      // getters and setters
  }
  </code></pre>
  
  <p>UserPostRating.java:</p>
  
  <pre><code>@Entity(name = "users_posts_ratings_map")
  public class UserPostRating {
      @EmbeddedId
      private RatingId userPost;
  
      @OneToOne
      @JoinColumn(name = "ratingId")
      private Rating rating;
  
      // getters and setters
  }
  </code></pre>
  
  <p>Post.java</p>
  
  <pre><code>@Entity(name = "posts")
  public class Post {
      @Id
      @Column(nullable = false)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      // irrelevant fields
  
      @ManyToMany
      @JoinTable(
              name = "users_posts_ratings_map",
              joinColumns = { @JoinColumn(name = "ratingId") },
              inverseJoinColumns = { @JoinColumn(name = "postId"), @JoinColumn(name = "userId") }
      )
      private Set&lt;UserPostRating&gt; ratings = new HashSet&lt;&gt;();
  
      // getters and setters
  }
  </code></pre>
  
  <p>I am getting </p>
  
  <pre><code>org.hibernate.MappingException: Foreign key (FKB278E73083D94769:users_posts_ratings_map [postId,userId])) must have same number of columns as the referenced primary key (users_posts_ratings_map [ratingId,postId,userId])
  </code></pre>
  
  <p>on servlet container initialization stage.</p>
  
  <p>What does it mean (What are Foreign Keys in this mappings? What are Primary Keys? Which annotations are marking what?) and how it could be fixed?</p>