我正在用Spring创建一个社交媒体网站,但是我对复合主键有疑问。 我有User,Post和PostLike表,我需要跟踪哪个用户喜欢哪些帖子。我也有一个UserData和Post关系,可以告诉我帖子作者,但这不会引起任何问题。基本上,我的PostLike表应该包含post_id和user_id列,它们应该用作复合主键。
我试图通过创建可嵌入的PostLikeId来做到这一点:
我的PostLikeId类:
@Embeddable
public class PostLikeId implements Serializable {
@Column(name = "post_id")
private Post post;
@Column(name = "user_id")
private UserData user;
// getters and setters
}
我的PostLike课程:
@Entity
@Table(name = "post_like")
public class PostLike {
@EmbeddedId
private PostLikeId id;
//getters and setters
}
我的帖子课程:
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_id_seq_generator")
@SequenceGenerator(name = "post_id_seq_generator", sequenceName = "post_id_seq", allocationSize = 10)
@Column(name = "id", updatable = false)
private Long id;
@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true
)
@JoinColumns({
@JoinColumn(name = "post_id",
referencedColumnName = "post_id"),
@JoinColumn(name = "user_id",
referencedColumnName = "user_id")
})
private List<PostLike> likes = new ArrayList<>();
}
...和我的UserData类:
@Entity
@Table(name = "user_data")
public class UserData {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_data_id_seq_generator")
@SequenceGenerator(name = "user_data_id_seq_generator", sequenceName = "user_data_id_seq", allocationSize = 10)
@Column(name = "id", updatable = false)
private Long id;
@OneToMany(
mappedBy = "user",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Post> posts = new ArrayList<>();
@OneToMany(
mappedBy = "id",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<PostLike> likedPosts = new ArrayList<>();
当我尝试运行并映射它时,出现以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to map collection com.budano.SocialStudent.model.Post.likes
.
.
.
Caused by: org.hibernate.AnnotationException: Unable to map collection com.budano.SocialStudent.model.Post.likes
.
.
.
Caused by: org.hibernate.cfg.RecoverableException: Unable to find column with logical name: post_id in org.hibernate.mapping.Table(public.post) and its related supertables and secondary tables
有人可以解释一下该日志告诉我什么,我应该如何解决此问题? 谢谢!
答案 0 :(得分:1)
这是您可以尝试的方法:
PostLikeId:
@Embeddable
public class PostLikeId implements Serializable {
@Column(name = "post_id")
private Long postId; // corresponds to PK type of Post
@Column(name = "user_id")
private Long userId; // corresponds to PK type of User
// ...
}
PostLike:
@Entity
@Table(name = "post_like")
public class PostLike {
@EmbeddedId
private PostLikeId id;
@MapsId("postId") // maps postId attribute of embedded id
@ManyToOne
private Post post;
@MapsId("userId") // maps userId attribute of embedded id
@ManyToOne
private UserData user;
// ...
}
帖子:
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_id_seq_generator")
@SequenceGenerator(name = "post_id_seq_generator", sequenceName = "post_id_seq", allocationSize = 10)
@Column(name = "id", updatable = false)
private Long id;
@ManyToOne
private UserData user;
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostLike> likes = new ArrayList<>();
// ...
}
UserData:
@Entity
@Table(name = "user_data")
public class UserData {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_data_id_seq_generator")
@SequenceGenerator(name = "user_data_id_seq_generator", sequenceName = "user_data_id_seq", allocationSize = 10)
@Column(name = "id", updatable = false)
private Long id;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostLike> likedPosts = new ArrayList<>();
// ...
}
在JPA 2.2 spec的第2.4.1节中讨论了派生的身份(带有示例)。