jointable中的java复合ID
我有以下类似PostLike的课程:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PostLike extends BaseEntity {
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "post_id")
private Post post;
}
该类已具有父BaseEntity类提供的ID字段
在User类中,我有以下字段:
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
Set<PostLike> userLikes = new HashSet<PostLike>();
在后课堂上:
@OneToMany(mappedBy = "post")
private Set<PostLike> postLikes = new HashSet<PostLike>();
我希望PostLike有一个复合主键,它由用户id和post id组成。 提前谢谢
# 1 楼答案
作为一种方法,您可以使用
@EmbeddedId
注释并使用可嵌入类表示此复合键:一些注释:
来自
@EmbeddedId
的javadoc从Java Persistence with Hibernate