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;
}

 

posted @ 2018-07-02 19:34  cearnach  阅读(118)  评论(0编辑  收藏  举报