title: JPA- Compound Primary Key using @EmbededId
body: <p><strong>Goal</strong>: To impolement an @Entity where the id is a compound primary key using @EmbededId.</p>
  
  <p><strong>Problem</strong>: Based on my current implementation, I am getting the following result:</p>
  
  <pre><code>[
    {
      "id": 1,
      "name": "Recipe 1",
      "instruction": "Test Instruction",
      "note": "Note 1",
      "show": true,
      "createDate": null,
      "modify_date": null,
      "ingredient": [
        {},
        {}
      ]
    }
  ]
  </code></pre>
  
  <p>but I want to have this: </p>
  
  <pre><code>[
    {
      "id": 1,
      "name": "Recipe 1",
      "instruction": "Test Instruction",
      "note": "Note 1",
      "show": true,
      "createDate": null,
      "modify_date": null,
      "ingredient": [
        {ingredient_id: 1,
         amount: 10},
        {ingredient_id: 2,
         amount: 20}
      ]
    }
  ]
  </code></pre>
  
  <p>Can someone please help me too see where I have done wrong in my recipeIngredient class? Thanks in advance.</p>
  
  <p><strong>The following are my implementation:</strong></p>
  
  <p>The schema:
  <a href="https://i.stack.imgur.com/NhUYD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NhUYD.png" alt="enter image description here"></a></p>
  
  <p>RecipeIngredientId.java</p>
  
  <pre><code>@Embeddable
  public class RecipeIngredientId implements Serializable {
      @Column(name = "recipe_id", nullable = false)
      private int recipeId;
  
      @Column(name = "ingredient_id", nullable = false)
      private int ingredientId;
  
      public RecipeIngredientId() {}
  
      public RecipeIngredientId(int recipeId, int ingredientId) {
          this.recipeId = recipeId;
          this.ingredientId = ingredientId;
      }
  }
  </code></pre>
  
  <p>RecipeIngredient.java</p>
  
  <pre><code>@Entity
  @Table(name = "recipe_ingredient")
  public class RecipeIngredient implements Serializable
  {
      @EmbeddedId
      private RecipeIngredientId id;
  
      @ManyToOne
      @JoinColumn(name="ingredient_id", insertable = false, updatable = false)
      private Ingredient ingredient;
      @ManyToOne
      @JoinColumn(name = "recipe_id", insertable = false, updatable = false)
      private Recipe recipe;
      private double amount;
  
      public RecipeIngredient() {}
  
      public RecipeIngredient(Recipe recipe, Ingredient ingredient, double amount){
          this.recipe = recipe;
          this.ingredient = ingredient;
          this.amount = amount;
      }
  }
  </code></pre>
  
  <p>Recipe.java:</p>
  
  <pre><code>@Entity
  public class Recipe {
      private int id;
  
      @NotNull
      private String name;
  
      private String instruction;
      private String note;
  
      @NotNull
      private boolean show;
  
      @CreationTimestamp
      @Temporal(TemporalType.DATE)
      @Column(name = "create_date")
      private Date createDate;
  
      @UpdateTimestamp
      @Temporal(TemporalType.TIMESTAMP)
      @Column(name = "modify_date")
      private Date modify_date;
  
      private Set&lt;RecipeIngredient&gt; recipeIngredients;
  
      public Recipe() {}
  
      public Recipe(String name, String instruction, String note, boolean show) {
          this.name = name;
          this.instruction = instruction;
          this.note = note;
          this.show = show;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getInstruction() {
          return instruction;
      }
  
      public void setInstruction(String instruction) {
          this.instruction = instruction;
      }
  
      public String getNote() {
          return note;
      }
  
      public void setNote(String note) {
          this.note = note;
      }
  
      public boolean isShow() {
          return show;
      }
  
      public void setShow(boolean show) {
          this.show = show;
      }
  
      public Date getCreateDate() {
          return createDate;
      }
  
      public void setCreateDate(Date createDate) {
          this.createDate = createDate;
      }
  
      public Date getModify_date() {
          return modify_date;
      }
  
      public void setModify_date(Date modify_date) {
          this.modify_date = modify_date;
      }
  
      @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL)
      public Set&lt;RecipeIngredient&gt; getIngredient() {
          return recipeIngredients;
      }
  
      public void setIngredient(Set&lt;RecipeIngredient&gt; recipeIngredients) {
          this.recipeIngredients = recipeIngredients;
      }
  }
  </code></pre>
  
  <p>Ingredient.java</p>
  
  <pre><code>@Entity
  public class Ingredient {
      private int id;
  
      @NotNull
      @Column(unique=true)
      private String name;
  
      private Set&lt;RecipeIngredient&gt; recipeIngredients;
  
      public Ingredient() {}
  
      public Ingredient(String name) {
          this.name = name;
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @OneToMany(mappedBy = "ingredient", cascade = CascadeType.ALL)
      public Set&lt;RecipeIngredient&gt; getRecipeIngredients() {
          return recipeIngredients;
      }
  
      public void setRecipeIngredients(Set&lt;RecipeIngredient&gt; recipeIngredients) {
          this.recipeIngredients = recipeIngredients;
      }
  }
  </code></pre>