title: JPA: primary key violation when saving an entity with many to one
body: <p>I have 2 JPA Entities</p>
  
  <pre><code>@Entity
  @Data
  public class Config {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      private String data;
  
      @ManyToOne(cascade= CascadeType.ALL)
      private Tenant tenant;
  }
  
  
  @Entity
  @AllArgsConstructor
  class Tenant {  
      @Id
      private String tenantID;
  }
  </code></pre>
  
  <p>and a repository</p>
  
  <pre><code>interface SolmanConfigPrivateRepository extends CrudRepository &lt;Config, Long&gt; {
  
  }
  </code></pre>
  
  <p>I create new entities with the following code</p>
  
  <pre><code>public void addConfig(Config config){
      String tenantId = userProvider.get();
      config.setTenant(getCurrentTenant());
      Tenant tenant = new Tenant(tenantId);
      dbRepository.save(config);
  
  }
  </code></pre>
  
  <p>I manage to save a Config using the save method of the repository.</p>
  
  <p>If I try to save a new Config with the same tenant, I get an error</p>
  
  <pre><code> Unique index or primary key violation: "PRIMARY_KEY_9 ON PUBLIC.TENANT(TENANTID)
  </code></pre>
  
  <p>How can I tell JPA to not try to create a new Tenant if there is already another one in the database (but to reference the existing one instead)?</p>