首页 > 解决方案 > compound key and CrudRepository in Spring boot

问题描述

I have the following entity :

@Entity
public class Attendance implements Serializable{

        private static final long serialVersionUID = 1L;
        @Id
        @ManyToOne
        private Student student;
        @Id
        @ManyToOne
        private Session session;
        ...

as you can see, the primary key is composed of two classes, and the CrudRepository accepts only one type as primary key (CrudRepository), how can I solve the problem please? Thank you.

标签: springspring-boot

解决方案


您需要一个复合键。你可以用不同的方式做到这一点。你可以使用@EmbeddedIdand@Embeddable或者你可以使用@IdClass(这里你的实体类中有多个@id)

另请参阅: 我应该使用哪个注释:@IdClass 或 @EmbeddedId


推荐阅读