0

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.

hereForLearing
  • 1,209
  • 1
  • 15
  • 33
  • Why do you want 2 PK in your table? A PK should be unique – akuma8 May 30 '18 at 01:12
  • because there is a many to many relation between Student and Session with extra fields, check this link : https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/ – hereForLearing May 30 '18 at 01:37
  • I read it in diagonal and I didn't see any entity with 2 `@Id`. I saw `@MapsId` which has different meaning. By the way, you can't have 2 `@Id` in the same entity. – akuma8 May 30 '18 at 01:45

1 Answers1

1

You need a composite key. You can do this in different ways. You can use @EmbeddedId and @Embeddable or you can use @IdClass (here you have more than one @id in your entity class)

See also: Which annotation should I use: @IdClass or @EmbeddedId

pL4Gu33
  • 2,045
  • 16
  • 38