title: Boot JPA Repository saveAndFlush with composite key
body: <p>I am currently trying to to perform a persist using a JPARepository interface of a Credential object, that has a composite key as a primary key.
  The repo is:</p>
  
  <p><code>@Transactional
           public interface CredentialRepository extends        JpaRepository&lt;Credential,CredentialPK&gt; {}</code></p>
  
  <p>The credentialPK is:</p>
  
  <pre><code>    @Embeddable
      public class CredentialPK implements Serializable {
  
      @Column(name = "CREDENTIAL_ID")
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer credentialID;
  
      @Column(name = "CREDENTIAL_TYPE_ID")
      private String credentialTypeID;
  
      public CredentialPK() {
      }
      //getters and setters;
  </code></pre>
  
  <p>}`</p>
  
  <p>The credential :
  <code>
       @Entity
       @Table(name = "TBL_CREDENTIALS")
      public class Credential {
          @EmbeddedId
          private CredentialPK credentialPK;
          //getters and setters
      }</code></p>
  
  <p>In the service class, I am trying to perform a save operation using a credential type, that is provided by the application. Let's say that for the example's sake, the credential type is 'temporary'. 
  The code for creating and persisting the credential is as follows:</p>
  
  <pre><code>     Credential credential = new Credential();
       CredentialPK pk = new CredentialPK(); 
       pk.setCredentialTypeID("temporary");
       credential.setCredentialPK(pk);
       Credential temporaryCredential =   credentialRepository.saveAndFlush(credential);
  
       temporaryCredential.getCredentialPK().getCredentialID(); // returns null, even when repository is flushed
  </code></pre>
  
  <p>`</p>
  
  <p>I require that the <strong>temporaryCredential</strong> have the credentialID populated with the id of the freshly inserted credential. However, the credentialID i'm getting is null. I've tried using both <strong>CrudRepository</strong> api and <strong>JpaRepository</strong> api for the repository implementations, with both <strong>save</strong> and <strong>saveAndFlush</strong> methods. This works perfectly if the credential's primary key is not composite, but due to legacy code, I cannot make this change.</p>
  
  <p>Any suggestions?</p>