title: Update the Foreign Key with JPA
body: <p>I created 2 entities : </p>
  
  <pre><code>@Entity
  @Table(name="products")
  public class ProductEntity {
  
      @Id
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      private Long id;
  
      @Column(nullable = false)
      private String productKeyId;
  
      // many to one relationship with category
      @ManyToOne
      @JoinColumn(name = "category_id")
      private CategoryEntity category;
  
      @Column(nullable = false)
      private String name;
  
      @Column(nullable = false)
      private double price;
  
      @Column(nullable = false)
      private int qty;
  
      private String imgPath;
  
      // getters &amp; setters
  }
  </code></pre>
  
  <p>And : </p>
  
  <pre><code>@Entity
  @Table(name="categories")
  public class CategoryEntity {
  
      @Id
      @GeneratedValue(strategy= GenerationType.IDENTITY)
      private Long id;
  
      @Column(length = 30, nullable = false)
      private String categoryKeyId;
  
      @Column(nullable = false)
      private String name;
  
      @ManyToOne(optional = true, fetch = FetchType.LAZY)
      @JoinColumn(name="parent_id", nullable=true)
      private CategoryEntity parentCategory;
  
      // allow to delete also subcategories
      @OneToMany(mappedBy="parentCategory", cascade = CascadeType.ALL)
      private List&lt;CategoryEntity&gt; subCategories;
  
      //Here mappedBy indicates that the owner is in the other side
      @OneToMany(fetch = FetchType.EAGER, mappedBy = "category", cascade = CascadeType.REMOVE)
      private List&lt;ProductEntity&gt; products;
  }
  </code></pre>
  
  <p>I have datas in the database generated : </p>
  
  <p>Here is my Product table</p>
  
  <p><a href="https://i.stack.imgur.com/jRPZl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jRPZl.png" alt="enter image description here"></a></p>
  
  <p>And the category table</p>
  
  <p><a href="https://i.stack.imgur.com/24AV4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/24AV4.png" alt="enter image description here"></a></p>
  
  <p>My issue is the following. I use a REST API to update the product and the category (if needed). </p>
  
  <pre><code>{
      "name": "Pizza12",
      "price": 25.0,
      "qty": 15,
      "imgPath": "anotherpathImage",
      "category": {
          "categoryKeyId": "VMz7EM6tNfoOAQtO1SHPYcH14jj0Cy",
          "name": "Fish"
      }
  }
  </code></pre>
  
  <p>In my service I try to update both part separatelly : </p>
  
  <pre><code>@Override
  public ProductDto updateProduct(String productKeyId, ProductDto productDto) {
      // create a return object of type Product
  
      ProductDto returnValue = new ProductDto();
  
      // create Entity objects to request on the database
      ProductEntity productEntity = productRepository.findByProductKeyId(productKeyId);
      CategoryEntity categoryEntity = categoryRepository.findCategoryEntityByProductKeyId(productKeyId);
  
      ModelMapper modelMapper = new ModelMapper();
  
      if (productEntity == null)
          throw new ApplicationServiceException(ErrorMessages.NO_RECORD_FOUND.getErrorMessage());
  
      productEntity.setProductKeyId(productKeyId);
      productEntity.setName(productDto.getName());
      productEntity.setPrice(productDto.getPrice());
      productEntity.setQty(productDto.getQty());
      productEntity.setImgPath(productDto.getImgPath());
  
      // update the category
      CategoryEntity updatedCategory = categoryRepository.save(categoryEntity);
      productEntity.setCategory(updatedCategory);
  
      // productEntity.setCategory(categoryEntity);
  
      System.out.println("product entity : " + productEntity.toString());
  
      ProductEntity updatedProduct = productRepository.save(productEntity);
  
      updatedProduct.setCategory(updatedCategory);
  
      returnValue = modelMapper.map(updatedProduct, ProductDto.class);
  
      return returnValue;
  }
  </code></pre>
  
  <p>Unfortunatelly, it doesn't seem to work as expected. The product is updated, the category remains the same. </p>