2 pièce(s) jointe(s)
spring doesnot Persist in database
Bonjour à tous,
j'ai un soucis avec mon service qui permet d'ajouter un exercise à une category.
Ma classe Exercise :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
@Entity
@Table(name = "exercise", schema = "public")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class Exercise implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name", columnDefinition = "TEXT")
@NotNull
private String name;
@Column( name="image")
private String image;
@Column(name = "duree")
private int duration;
@Column(name = "caloriesBurned")
private double caloriesBurned;
@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.PERSIST)
@JoinColumn(name = "categoryexericse_id", referencedColumnName = "id")
@JsonIgnoreProperties("exercisesList")
private CategoryExercise category;
} |
Ma classe CategoryExercise :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
@Entity
@Table(name = "categoryexercise", schema = "public")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "idcategoryexercise")
public class CategoryExercise implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name")
@NotNull
private String name;
@OneToMany( fetch = FetchType.LAZY, mappedBy = "category", cascade = CascadeType.PERSIST)
@Column(name="categroy")
@OrderBy("name ASC")
@JsonIgnoreProperties("categroy")
private List<Exercise> exercisesList;
@Column(name = "description",columnDefinition="TEXT")
private String description;
@Column( name="image")
private String image;
} |
Dans service , ExerciseService :
Code:
1 2 3 4 5
|
public Exercise addExercise( Exercise exercise ) {
return exerciseRepository.save(exercise);
} |
ExerciseController, premier essai :
Code:
1 2 3 4 5 6 7
|
@PostMapping("/addExercise")
@PreAuthorize("hasRole('ADMIN')")
public Exercise addExercise( @Valid @RequestBody Exercise exercise ) {
return exerciseService.addExercise(exercise);
} |
Deuxième essai :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@PostMapping("/categoryExercise/{id}/addExercise")
@PreAuthorize("hasRole('ADMIN')")
public Exercise addExercise( @Valid @RequestBody Exercise exercise ,@PathVariable ( "id") Long id ) {
CategoryExercise category = categoryExerciseService.getOneById(id);
List<Exercise> exercisesList = category.getExercisesList();
exercisesList.add(exercise);
category.setExercisesList(exercisesList);
return exerciseService.addExercise(exercise);
} |
Test avec postaman => même résulat, colonne category est toujours à null.
Pièce jointe 566528
Pièce jointe 566530