具有以下 SQL 表:
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)
);
并遵循 JPA 注释的 POJO:
RatingId.java:
@Embeddable
public class RatingId implements Serializable {
@ManyToOne
@JoinColumn(name = "userId")
private User user;
@ManyToOne
@JoinColumn(name = "postId")
private Post post;
// getters and setters
}
UserPostRating.java:
@Entity(name = "users_posts_ratings_map")
public class UserPostRating {
@EmbeddedId
private RatingId userPost;
@OneToOne
@JoinColumn(name = "ratingId")
private Rating rating;
// getters and setters
}
Post.java
@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<UserPostRating> ratings = new HashSet<>();
// getters and setters
}
我正进入(状态
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])
在 servlet 容器初始化阶段。
这是什么意思(这个映射中的外键是什么?主键是什么?哪些注释标记了什么?)以及如何修复它?