title: JPA: @ManyToOne, unnecessary duplicate Entries in 1 Table
body: <p>I have a dealersearch, where distributors are persisted. Data of dealers like street, name, phone number and so on will be imported somehow via excel files. </p>
  
  <p><strong>In my database I need meta-data about the imports of dealer data.</strong></p>
  
  <p>I use <strong>spring-boot-starter-jpa</strong>.</p>
  
  <h2>My models:</h2>
  
  <p>Import (metadata of the import[-process] itself)</p>
  
  <ul>
  <li>datetime</li>
  <li>status (successful/failed)</li>
  <li>numberOfDealers imported</li>
  <li>tenant (dealers of what brand/country e.g. Volkswagen in USA)</li>
  <li>environment (what servers: production, test, dev)</li>
  </ul>
  
  <p>Tenant</p>
  
  <ul>
  <li>brand (Audi, Volkswagen, Seat)</li>
  <li>market (country where the distributor is located)</li>
  </ul>
  
  <p>Import and Tenant are connected. Each Import includes one Tenant. One tenant Id appears multiple times in the Import table. Each Tenant in the Tenant table consisting of brand and market/country should be unique.</p>
  
  <p>Data rows example, <strong>how it should be</strong>:</p>
  
  <h2>Import</h2>
  
  <p>id - datetime - environment - numberOfDealers - status - tenant_id</p>
  
  <ol>
  <li>2016-04-17 - DEV - 122 - successful - 1</li>
  <li>2016-04-17 - DEV - 40 - successful - 2</li>
  <li>2016-04-19 - DEV - 130 - failed - 1</li>
  <li>2016-04-22 - LIVE - 1044 - successful - 3</li>
  </ol>
  
  <h2>Tenant</h2>
  
  <p>tenant_id - brand - market</p>
  
  <ol>
  <li>A - KOR</li>
  <li>A - ARG</li>
  <li>A - USA</li>
  </ol>
  
  <p><strong>How it actually is</strong>: </p>
  
  <h2>Tenant</h2>
  
  <p>tenant_id - brand - market</p>
  
  <ol>
  <li>A - KOR</li>
  <li>A - ARG</li>
  <li>A - KOR</li>
  <li>A - USA</li>
  </ol>
  
  <p>The relationship is correct, but Tenant has duplicates, <strong>KOR was entered twice</strong>.</p>
  
  <h2>My Java Models</h2>
  
  <pre><code>@Entity
  public class Import {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "import_id")
  private Long id;
  
  private String datetime;
  
  private String status;
  
  private Integer numberOfDealers;
  
  @ManyToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "tenant_id")
  private Tenant tenant;
  
  private String environment;
  
  public Import() { }
  
  public Import(String datetime, String status, Integer numberOfDealers, Tenant tenant, String environment) {
      this.datetime = datetime;
      this.status = status;
      this.numberOfDealers = numberOfDealers;
      this.tenant = tenant;
      this.environment = environment;
  }
  
  /*getters and setters here*/
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package com.dasburo.model;
  
  import com.fasterxml.jackson.annotation.JsonProperty;
  
  import javax.persistence.*;
  import java.util.List;
  
  @Entity
  //@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"market", "brand"})})
  public class Tenant {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "tenant_id")
  private Long tenantId;
  
  private String market;
  
  private String brand;
  
  public Tenant() { }
  
  public Tenant(String market, String brand) {
      this.market = market;
      this.brand = brand;
  } 
  
  /*getters and setters here*/
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>for (Import anImport : imports) {
      importDao.save(anImport);
  }
  </code></pre>
  
  <p>imports is an array of Import objects filled with data, including a tenant</p>
  
  <p>The use of uniqueConstraints annotation does only set the structure of the table 'Tenant', but not the behaviour. JPA still tries to do duplicate entries in the 'Tenant' table.</p>