Hibernate 组合主键的使用

使用@Embeddable注解,组合键的类必须实现 Serializable 接口
@Data
@Embeddable
public class UserCommentId implements Serializable {
    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;

    @OneToOne
    @JoinColumn(name = "comment_id")
    private Comment comment;
}

使用:

@Data
@Entity
public class UserComment {
    @EmbeddedId
    private UserCommentId userCommentId;
}
原文地址:https://www.cnblogs.com/cearnach/p/9255429.html