1

i want to implement a many to many association with quantity information in it . like this :

@Entity
@Table(name = "reserves")
@Getter @Setter @NoArgsConstructor
public class Reserve {
    @Id
    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "groupe_id")
    private GroupeSanguin bloodGroup;
    @Id
    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    private Banque banque;
    private int quantity;
}

the GroupSanguin and the Banque are two class stored in the database two . here is the code for the two if you need :

@Entity
@Table(name = "groupe_sanguins")
public class GroupeSanguin {
    @Id
    private String groupe;
    @OneToMany(mappedBy = "groupeSanguin")
    private List<Donneur> donneurs;
}
@Entity @Getter @Setter @NoArgsConstructor
public class Banque {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true,nullable = false)
    private String nom;
    private String adresse;
    @Column(unique = true)
    private String telephone;
    private String localisation;
}

so my i want to know how to annotate the JpaRepository to take the two as primary key like this and is my annotation good for it to work ?

public interface ReserveRepository extends JpaRepository<
Reserve,
//what to put here ?
>
Youtech code
  • 101
  • 7

3 Answers3

1

This isn't a JPA question in fact, it's a relationnal database conception. If Reserve has is own data and links with other entity it has it own Id You can add unicity constraint

@Entity
@Table(name = "reserves", uniqueConstraints={
@UniqueConstraint(columnNames = {"banque_id", "groupe_id"})
@Getter @Setter @NoArgsConstructor
public class Reserve {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
   

@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "groupe_id")
private GroupeSanguin bloodGroup;
   
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "banque_id")
private Banque banque;
private int quantity;

}

Mr_Thorynque
  • 1,749
  • 1
  • 20
  • 31
0

i've found this solutions too.

@Entity
@Table(name = "reserves")
@Getter @Setter @NoArgsConstructor
@IdClass(ReserveId.class) //this annotation will tell that id that the 
// the id will be represented by a class
public class Reserve {
    @Id
    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "groupe_id")
    private GroupeSanguin groupeSanguin;
    @Id
    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name = "banque_id")
    private Banque banque;
    private int quantity;
}

and the id class should implements Serializable like this :

@Getter @Setter
public class ReserveId implements Serializable {
    private Banque banque;
    private GroupeSanguin groupeSanguin;
}

and finally the repository will be like that :

@Repository
public interface ReserveRepo extends JpaRepository<Reserve, ReserveId>{}
Youtech code
  • 101
  • 7
0

See your Reserve class has nowhere mentioned composite primary key. First you need to fix the model, You can refer to the solution here How to create and handle composite primary key in JPA