@Entity
@IdClass(SamplingsPK.class)
public class Samplings {
@Id
private Integer year;
@Id
@GeneratedValue
private Integer id;
}
public class SamplingsPK implements Serializable {
private int year;
private Integer id;
public SamplingsPK(int year, Integer id) {
this.id = id;
this.year=year;
}
private SamplingsPK(){
}
@PrePersist
public void prePersist() {
year = LocalDate.now().getYear();
}
}
Puisque nous utilisons une clé composé, au niveau du repository, il faut spécifié le type.
@Repository
public interface SamplingsRepository extends JpaRepository<Samplings, SamplingsPK> {
}
Lorsque vous utilisez le paging dans Spring Data, il faut mentionner les champs de la clé composé.
Pageable pageable = PageRequest.of(1, 1, Sort.Direction.ASC, "year","id");
samplingsRepository.findAll(pageable);
Nous avons vue comment utiliser une clé composé lors de l'utilisation de la pagination avec Spring data.
C'est un très bon article, simple et clair :)
RépondreSupprimer