title: Spring JPA - How to Save Object with a Composite Key (EmbeddedID)
body: <p>I'm building a Spring system which involves users and the medication they are taking.
  I have a database with the tables:
  users, drugs</p>
  
  <p>I am creating a new datatype names UserMed which is composed of a composite primary key - a drug's id and a user's username (the primary keys of the tables above)</p>
  
  <p>Here is the UserMed entity code:</p>
  
  <pre><code>@Entity
  @Table(name = "userMeds")
  public class UserMed implements Serializable {
  
      @Id
      @EmbeddedId
      private UserMedId userMedId;
  
  
      public UserMed(int drugID, String username) {
          this.userMedId.drug_id = drugID;
          this.userMedId.username = username;
  
      }
  
      public UserMed() {
      }
  
  
      public String getUsername() {
          return this.userMedId.username;
      }
  
      public void setUsername(String username) {
          this.userMedId.username = username;
      }
  
  
  }
  </code></pre>
  
  <p>And here is the EmbeddedID UserMedId data type:</p>
  
  <pre><code>@RequiredArgsConstructor
  @NoArgsConstructor
  @Getter
  @Setter
  @ToString
  @EqualsAndHashCode
  @Embeddable
  public class UserMedId implements Serializable {
  
      @NonNull
      public int drug_id;
  
      @NonNull
      public String username;
  }
  </code></pre>
  
  <p>I have a route, which my frontend POST requests to.
  I have double checked the front end, and it is doing the POST request with a serialised UserMed type, which looks like this:</p>
  
  <pre><code>export class UserMed {
      constructor(
          public drug_id: number,
          public username: string,
      ) {
      }
  }
  </code></pre>
  
  <p>But hitting the route with a userMed type in the request body, is causing this error:</p>
  
  <pre><code>Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: N/A
   at [Source: (PushbackInputStream); line: 1, column: 28] (through reference chain: com.example.configbackendspring.entities.UserMed["username"])]
  </code></pre>
  
  <p>Here is the POST mapping/route that is hit</p>
  
  <pre><code>    @PostMapping("/addNewUserMed")
      public String addNewUser(@RequestBody UserMed userMed) {
          userMedService.addNewUserMed(userMed);
          return "success";
      }
  </code></pre>
  
  <p>And here is the addNewUserMed method:</p>
  
  <pre><code>   public void addNewUserMed(UserMed userMed) {
          userMedRepository.save(userMed);
      }
  
  </code></pre>
  
  <p>I think the issue is that since it's a composite key which is used, the UserMed which is being sent from the frontend contains field 'username' and 'drug_id', but the Spring UserMed data type has a composite key and so only has fields 'userMedId.drug_id' and 'userMedId.username'.</p>
  
  <p>Can someone please advise how to make the save operation work correctly?</p>
  
  <p>Thank you</p>