1. 程式人生 > >Hibernate 組合主鍵的使用

Hibernate 組合主鍵的使用

div too pri code onetoone vat embedded ble clas

使用@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; }

Hibernate 組合主鍵的使用