title: When I add an Entity with hibernate the foreign key is always saved as null
body: <p>I have two classes Panier.java and Categorie.java with OneToMany relationship. When I post via postman a panier with category id, all the data are stored correctly except the category in the DB and the foreign key in the panier table is stored always as null.</p>
  <p>Panier.java</p>
  <pre><code>package com.app.saveeat.entity;
  
  import com.fasterxml.jackson.annotation.JsonBackReference;
  import com.fasterxml.jackson.annotation.JsonIgnore;
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.NoArgsConstructor;
  
  import javax.persistence.*;
  import java.io.Serializable;
  import java.time.LocalDateTime;
  
  @Entity
  @AllArgsConstructor
  public class Panier implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(nullable = false, updatable = false)
      private Long id;
      private String nom;
      private String description;
      private Double prixInitial;
      private Double prixOffre;
      private String quantity;
      private LocalDateTime dateDebut;
      private LocalDateTime dateFin;
      private String image;
      private String adresse;
      @Column(nullable = false,updatable = false)
      private String codePanier;
  
  
  
      @JsonIgnore
      @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      @JoinColumn (name = &quot;categorie&quot;)
      private Categorie categorie;
  
      public Panier(){}
      public Panier(String nom, String description, Double prixInitial, Double prixOffre, String quantity, LocalDateTime dateDebut,LocalDateTime dateFin, String image, String adresse, String codePanier){
          this.nom = nom;
          this.description = description;
          this.prixInitial = prixInitial;
          this.prixOffre = prixOffre;
          this.quantity = quantity;
          this.dateDebut = dateDebut;
          this.image = image;
          this.adresse = adresse;
          this.codePanier = codePanier;
          this.dateFin = dateFin;
      }
  
      public Long getId(){
          return id;
      }
  
      public void setId(Long id){
          this.id = id;
      }
  
      public String getNom(){
          return nom;
      }
  
      public void setNom(String nom){
          this.nom = nom;
      }
  
      public String getDescription(){
          return description;
      }
  
      public void setDescription(String description){
          this.description = description;
      }
  
      public Double getPrixInitial(){
          return prixInitial;
      }
  
      public void setPrixInitial(Double prixInitial){
          this.prixInitial = prixInitial;
      }
  
      public Double getPrixOffre(){
          return prixOffre;
      }
  
      public void setPrixOffre(Double prixOffre){
          this.prixOffre = prixOffre;
      }
  
      public String getQuantity(){
          return quantity;
      }
  
      public void setQuantity(String quantity){
          this.quantity = quantity;
      }
  
      public LocalDateTime getDateDebut(){
          return dateDebut;
      }
  
      public void setDateDebut(LocalDateTime dateDebut){
          this.dateDebut = dateDebut;
      }
  
      public LocalDateTime getDateFin() { return dateFin; }
  
      public void setDateFin(LocalDateTime dateFin) {
          this.dateFin = dateFin;
      }
  
      public String getImage(){
          return image;
      }
  
      public void setImage(String image){
          this.image = image;
      }
  
      public String getAdresse(){
          return adresse;
      }
  
      public void setAdresse(String adresse){
          this.adresse = adresse;
      }
  
      public String getCodePanier(){
          return codePanier;
      }
  
      public void setCodePanier(String codePanier){
          this.codePanier = codePanier;
      }
  
      public Categorie getCategorie() {
          return categorie;
      }
  
      public void setCategorie(Categorie categorie) {
          this.categorie = categorie;
      }
  
      @Override
      public String toString(){
          return &quot;Panier{&quot; +
                  &quot;id=&quot; + id +
                  &quot;, nom='&quot; + nom + '\'' +
                  &quot;, description='&quot; + description + '\'' +
                  &quot;, prixInitial='&quot; + prixInitial + '\'' +
                  &quot;, prixOffre='&quot; + prixOffre + '\'' +
                  &quot;, quantity='&quot; + quantity + '\'' +
                  &quot;, dateDebut='&quot; + dateDebut + '\'' +
                  &quot;, dateFin='&quot; + dateFin + '\'' +
                  &quot;, image='&quot; + image + '\'' +
                  &quot;, adresse='&quot; + adresse + '\'' +
                  &quot;, categorie='&quot; + categorie + '\'' +
                  '}';
      }
  }
  
  </code></pre>
  <p>Categorie.java</p>
  <pre><code>package com.app.saveeat.entity;
  
  import com.fasterxml.jackson.annotation.JsonIgnore;
  import lombok.AllArgsConstructor;
  import lombok.Data;
  import lombok.NoArgsConstructor;
  
  import javax.persistence.*;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  
  
  @Entity
  @Data
  @AllArgsConstructor
  public class Categorie implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(nullable = false, updatable = false)
      private Long id;
      private String nom;
  
      @JsonIgnore
      @OneToMany(mappedBy = &quot;categorie&quot;,cascade = CascadeType.ALL)
      private List&lt;Panier&gt; paniers;
  
      public List&lt;Panier&gt; getPaniers() {
          return paniers;
      }
  
      public void setPaniers(List&lt;Panier&gt; paniers) {
          this.paniers = paniers;
      }
  
      public Categorie() {}
      public Categorie(Long id, String nom) {
          this.id = id;
          this.nom = nom;
      }
  
  
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getNom() {
          return nom;
      }
  
      public void setNom(String nom) {
          this.nom = nom;
      }
  }
  
  </code></pre>
  <p>PanierController.java</p>
  <pre><code>package com.app.saveeat.controller;
  
  import com.app.saveeat.entity.Categorie;
  import com.app.saveeat.entity.Panier;
  import com.app.saveeat.service.PanierService;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.*;
  import java.util.List;
  
  
  @CrossOrigin(origins = &quot;*&quot;)
  @RestController
  @RequestMapping(value = &quot;/panier&quot;,
          headers = &quot;Accept=application/json&quot;,
          method = { RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE })
  public class PanierController {
      private final PanierService panierService;
  
      public PanierController(PanierService panierService) {
          this.panierService = panierService;
      }
  
      @GetMapping(&quot;/all&quot;)
      public ResponseEntity&lt;List&lt;Panier&gt;&gt; getAllPaniers(){
          List&lt;Panier&gt; paniers = panierService.findAllPaniers();
          return new ResponseEntity&lt;&gt;(paniers, HttpStatus.OK);
      }
  
      @GetMapping(&quot;/find/{id}&quot;)
      public ResponseEntity&lt;Panier&gt; getPanierById(@PathVariable(&quot;id&quot;) Long id){
          Panier panier = panierService.findPanierById(id);
          return new ResponseEntity&lt;&gt;(panier, HttpStatus.OK);
      }
  
      @PostMapping(&quot;/add&quot;)
      public ResponseEntity&lt;Panier&gt; addPanier(@RequestBody Panier panier){
          Panier newPanier = panierService.addPanier(panier);
          return new ResponseEntity&lt;&gt;(newPanier, HttpStatus.CREATED);
      }
  
      @PutMapping(&quot;/update&quot;)
      public ResponseEntity&lt;Panier&gt; updatePanier(@RequestBody Panier panier){
          Panier updatePanier = panierService.updatePanier(panier);
          return new ResponseEntity&lt;&gt;(updatePanier, HttpStatus.OK);
      }
  
      @DeleteMapping(&quot;/delete/{id}&quot;)
      public ResponseEntity&lt;?&gt; deletePanier(@PathVariable(value = &quot;id&quot;) Long id){
          panierService.deletePanier(id);
          return new ResponseEntity&lt;&gt;(HttpStatus.OK);
      }
  
  
  }
  
  </code></pre>
  <p>CategorieController.java</p>
  <pre><code>package com.app.saveeat.controller;
  
  import com.app.saveeat.entity.Categorie;
  import com.app.saveeat.service.CategorieService;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.*;
  
  import java.util.List;
  
      @CrossOrigin(origins = &quot;*&quot;)
      @RestController
      @RequestMapping(value = &quot;/categorie&quot;)
      public class CategorieController {
      private final CategorieService categorieService;
      public CategorieController(CategorieService categorieService) {
          this.categorieService = categorieService;
      }
  
      @GetMapping(&quot;/all&quot;)
      public ResponseEntity&lt;List&lt;Categorie&gt;&gt; getAllCategories(){
          List&lt;Categorie&gt; categories = categorieService.findAllCategories();
          return new ResponseEntity&lt;&gt;(categories, HttpStatus.OK);
      }
  
      @GetMapping(&quot;/find/{id}&quot;)
      public ResponseEntity&lt;Categorie&gt; getCategorieById(@PathVariable(&quot;id&quot;) Long id){
          Categorie categorie = categorieService.findCategorieById(id);
          return new ResponseEntity&lt;&gt;(categorie, HttpStatus.OK);
      }
  }
  
  
  
  </code></pre>
  <p>PanierService.java</p>
  <pre><code>package com.app.saveeat.service;
  
  import com.app.saveeat.entity.Panier;
  import com.app.saveeat.exception.PanierNotFoundException;
  import com.app.saveeat.repository.PanierRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import javax.transaction.Transactional;
  import java.util.List;
  import java.util.UUID;
  
  @Service
  @Transactional
  public class PanierService {
      private final PanierRepository panierRepository;
  
      @Autowired
      public PanierService(PanierRepository panierRepository) {
          this.panierRepository = panierRepository;
      }
  
      public Panier addPanier(Panier panier){
          panier.setCodePanier(UUID.randomUUID().toString());
          return panierRepository.save(panier);
      }
  
      public List&lt;Panier&gt; findAllPaniers(){
          return panierRepository.findAll();
      }
  
      public Panier updatePanier(Panier panier){
          return panierRepository.save(panier);
      }
  
      public Panier findPanierById(Long id){
          return  panierRepository.findPanierById(id).orElseThrow(() -&gt; new PanierNotFoundException(&quot;Panier by id&quot; + id + &quot;was not found&quot;));
      }
  
      public void deletePanier(Long id){
          panierRepository.deletePanierById(id);
      }
  }
  
  
  
  </code></pre>
  <p>CategorieService.java</p>
  <pre><code>package com.app.saveeat.service;
  
  import com.app.saveeat.entity.Categorie;
  import com.app.saveeat.exception.CategorieNotFoundException;
  import com.app.saveeat.repository.CategorieRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import javax.transaction.Transactional;
  import java.util.List;
  
  @Service
  @Transactional
  public class CategorieService {
      private final CategorieRepository categorieRepository;
  
      @Autowired
      public CategorieService(CategorieRepository categorieRepository) {
          this.categorieRepository = categorieRepository;
      }
  
      public List&lt;Categorie&gt; findAllCategories(){
          return categorieRepository.findAll();
      }
      public Categorie findCategorieById(Long id){
          return  categorieRepository.findCategorieById(id).orElseThrow(() -&gt; new CategorieNotFoundException(&quot;Categorie by id&quot; + id + &quot;was not found&quot;));
      }
  }
  
  </code></pre>
  <p>postman api test
  Request</p>
  <pre><code>{
      &quot;nom&quot;: &quot;panier1 &quot;,
      &quot;description&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit&quot;,
      &quot;prixInitial&quot;: 11,
      &quot;prixOffre&quot;:10,
      &quot;quantity&quot;: &quot;12&quot;,
      &quot;dateDebut&quot;: &quot;2022-03-30T19:00:00.000Z&quot;,
      &quot;dateFin&quot;: &quot;2022-03-30T21:30:15.000Z&quot;,
      &quot;image&quot;: &quot;https://bootdey.com/img/Content/avatar/avatar4.png&quot;,
      &quot;adresse&quot;: &quot;France&quot;,
      &quot;codePanier&quot;: &quot;4570090332&quot;,
      &quot;categorie&quot;: {
          &quot;id&quot;: 2
      }
  }
  </code></pre>
  <p>Response</p>
  <pre><code>{
      &quot;nom&quot;: &quot;panier1 &quot;,
      &quot;description&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit&quot;,
      &quot;prixInitial&quot;: 11,
      &quot;prixOffre&quot;:10,
      &quot;quantity&quot;: &quot;12&quot;,
      &quot;dateDebut&quot;: &quot;2022-03-30T19:00:00.000Z&quot;,
      &quot;dateFin&quot;: &quot;2022-03-30T21:30:15.000Z&quot;,
      &quot;image&quot;: &quot;https://bootdey.com/img/Content/avatar/avatar4.png&quot;,
      &quot;adresse&quot;: &quot;France&quot;,
      &quot;codePanier&quot;: &quot;f4466839-c498-4c2a-b1de-48c184aed450&quot;
  }
  any help please ?
  
  </code></pre>