I'm using Springboot and MySQL in order to make an API, I'm currently facing a problem, and I cannot find any answers on the internet :
The problem is in my association Table "EtapeRecette.java" below i try to initialize the getters and setters with Lombok, but when i use @Data, i get no errors but when I findAll() with the RecetteRepository, i get an array of "etapes" empty, but if I set the getters and setters in "EtapeRecette.java" manually it works all good, same if i use @Getters and @Setters, is there any reasons @Data doesnt work?
I got 3 tables :
Recette.java
package org.microferme.charme.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Set;
@Data
@NoArgsConstructor
@Entity
@Table(name="Recette")
public class Recette {
@Id
private String id;
@Column(name = "nom")
private String nom;
@Column(name = "nb_personne")
private int nbPersonne;
@Column(name = "prix_par_personne")
private float prixParPersonne;
@OneToMany(mappedBy = "recette", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties("recette")
private Set<MediaRecette> medias;
@OneToMany(mappedBy = "recette", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties("recette")
private Set<StockUtile> ingredients;
@OneToMany(mappedBy = "recette", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties("recette")
private Set<EtapeRecette> etapes;
public Recette(String nom, int nbPersonne, float prixParPersonne) {
//Si l'id n'est pas spécifié on prend la timestamp actuelle
Date date = new Date();
this.id = String.valueOf(new Timestamp(date.getTime()).getTime());
this.nom = nom;
this.nbPersonne = nbPersonne;
this.prixParPersonne = prixParPersonne;
}
public void addMedia(MediaRecette media) {
this.medias.add(media);
}
public void addIngredient(StockUtile ingredient) {
this.ingredients.add(ingredient);
}
public void addEtape(EtapeRecette etapeRecette) {
this.etapes.add(etapeRecette);
}
}
Etape.java
package org.microferme.charme.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Set;
@Data
@Entity
@Table(name = "Etape")
public class Etape {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "ordre")
private float ordre;
@Column(name = "duree")
private float duree;
@Column(name = "description")
private String description;
@ManyToOne
private Ustensile ustensile;
public Etape() {
}
public Etape(float ordre, float duree, String description, Ustensile ustensile) {
this.ordre = ordre;
this.duree = duree;
this.description = description;
this.ustensile = ustensile;
}
}
EtapeRecette which is an association table between Recette and Etape in order to have a many to many relationship
package org.microferme.charme.model;
import jakarta.persistence.*;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.microferme.charme.model.embeddable.EtapeRecetteId;
@Entity
@Data
@Table(name = "EtapeRecette")
public class EtapeRecette {
@EmbeddedId
private EtapeRecetteId id;
@ManyToOne(cascade = CascadeType.ALL)
@MapsId("recetteId")
private Recette recette;
@ManyToOne(cascade = CascadeType.ALL)
@MapsId("etapeId")
private Etape etape;
public EtapeRecette() {
}
public EtapeRecette(Recette recette, Etape etape) {
this.id = new EtapeRecetteId(recette.getId(), etape.getId());
this.recette = recette;
this.etape = etape;
}
}
The difference is that
@Datagenerates not onlygettersandsettersbut also theequalsandhashCodemethods. The default implementation of these methods does not work well with Hibernate relationships like one-to-many and many-to-one. You are better of if you use a combination of@Getter@Setterand@EqualsAndHashCode(onlyExplicitlyIncluded = true)and add@EqualsAndHashCode.Includeto the ID field. Like this:Based on: https://thorben-janssen.com/lombok-hibernate-how-to-avoid-common-pitfalls/