title: How to persist data on many side of relationship with composite primary keys on many side of relationship
body: <p>I am stuck with a problem and any help would be highly appreciated.
  I have a one to may relationship entities with mostly having composite primary keys on many side of relationship.
  e.g I have two entities like <code>Client</code> and <code>Client_User</code> and <code>Client</code> is associated with <code>Client_User</code> which is having composite primary key i-e, <code>client_id</code> and <code>user_id</code>, I want to persist data to <code>Client_User</code> Entity when I am persisting data to <code>Client</code> Entity using Spring data Jpa. My entities are written as below:</p>
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Table(name = &quot;client&quot;)
  public class ClientEntity implements Serializable {
     private int clientId;
     private int userId;
     private Collection&lt;ClientUserEntity&gt; clientUsersByClientId;
  
     // ...
     
     @OneToMany(mappedBy = &quot;clientByClientId&quot;, fetch = FetchType.LAZY)
     @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
     public Collection&lt;ClientUserEntity&gt; getClientUsersByClientId() {
          return clientUsersByClientId;
     }
     public void setClientUsersByClientId(Collection&lt;ClientUserEntity&gt; clientUsersByClientId) {
          this.clientUsersByClientId = clientUsersByClientId;
     }
  }
      
  @Entity
  @Table(name = &quot;client_user&quot;)
  @IdClass(ClientUserEntityPK.class)
  public class ClientUserEntity implements Serializable {
     private int clientId;
     private int userId;
          
     @Id
     @Column(name = &quot;client_id&quot;)
     public int getClientId() {
         return clientId;
     }
     public void setClientId(int clientId) {
         this.clientId = clientId;
     }
          
     @Id
     @Column(name = &quot;user_id&quot;)
     public int getUserId() {
        return userId;
     }
     public void setUserId(int userId) {
        this.userId = userId;
     }
    
     @ManyToOne
     @JoinColumn(name = &quot;client_id&quot;, referencedColumnName = &quot;client_id&quot;, nullable = false,insertable =false ,updatable  =false)
     public ClientEntity getClientByClientId() {
        return clientByClientId;
     }
     public void setClientByClientId(ClientEntity clientByClientId) {
        this.clientByClientId = clientByClientId;
     }
  }
          
  public class ClientUserEntityPK implements Serializable {
     private int clientId;
     private int userId;
      
     @Column(name = &quot;client_id&quot;)
     @Id
     public int getClientId() {
        return clientId;
     }
     public void setClientId(int clientId) {
        this.clientId = clientId;
     }
      
     @Column(name = &quot;user_id&quot;)
     @Id
     public int getUserId() {
         return userId;
     }
     public void setUserId(int userId) {
         this.userId = userId;
     }
      
     @Override
     public boolean equals(Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         ClientUserEntityPK that = (ClientUserEntityPK) o;
         return clientId == that.clientId &amp;&amp;
                userId == that.userId;
     }
      
     @Override
     public int hashCode() {
         return Objects.hash(clientId, userId);
     }
  }
  </code></pre>