title: Java Spring Hibernate Saving Duplicate Values to MySQL table is throwing error
body: <p>I am trying to save some values to a MYSQL database using CrudRepository but doesnt seem to work. I have tried the following guide <a href="https://spring.io/guides/gs/accessing-data-mysql/" rel="nofollow noreferrer">https://spring.io/guides/gs/accessing-data-mysql/</a> and it works fine but when iam trying to follow the same principle for my program it doesnt seem to work for some reason. </p>
  
  <p>Here is the class iam creating a database for, The table gets created properly:</p>
  
  <pre><code> @Entity
   public class MeasurementPoint {
  
   @Id
  
  
   @JsonProperty("f1")
   private double f1;
  
   @JsonProperty("precison")
   private double precison;
  
   @JsonProperty("recall")
   private double recall;
  
   private String date;
  
   public MeasurementPoint(double f1, double precison, double recall, String 
   date)  {
       this.f1 = f1;
       this.precison = precison;
       this.recall = recall;
       this.date=date;
   }
  
   public double getF1() {
       return f1;
   }
  
   public double getPrecison() {
       return precison;
   }
  
   public String getDate() {
       return date;
   }
   public double getRecall() {
       return recall;
   }
  }
  </code></pre>
  
  <p>Here is the MSQL terminal outputting the columns for the entity created:</p>
  
  <pre><code>+----------+--------------+------+-----+---------+----------------+
  | Field    | Type         | Null | Key | Default | Extra          |
  +----------+--------------+------+-----+---------+----------------+
  | f1       | double       | NO   | PRI | NULL    | auto_increment |
  | date     | varchar(255) | YES  |     | NULL    |                |
  | precison | double       | NO   |     | NULL    |                |
  | recall   | double       | NO   |     | NULL    |                |
  +----------+--------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>Here is the class iam trying to add values to the database from:</p>
  
  <pre><code>@Controller
  public class DataChart {
  
  private static final Logger logger= LoggerFactory.getLogger(DataChart.class);
  
  @Autowired
  private static MeasurementRepository measurementRepository;
  
  @GetMapping(value = "/statementchart")
  @ResponseBody
  public List&lt;Measurement&gt; scoreChartData() {
  
      List&lt;MeasurementPoint&gt; needDataPoints = new ArrayList&lt;&gt;();
      List&lt;MeasurementPoint&gt; backgroundDataPoints = new ArrayList&lt;&gt;();
      List&lt;MeasurementPoint&gt; goalDataPoints=new ArrayList&lt;&gt;();
      needDataPoints.add(new MeasurementPoint(0.3, 0.5, 0.2,  "2017-11-19"));
      needDataPoints.add(new MeasurementPoint(0.7, 0.4, 0.15, "2017-11-12"));
      needDataPoints.add(new MeasurementPoint(0.5, 0.3, 0.10, "2017-11-15"));
      needDataPoints.add(new MeasurementPoint(0.6, 0.2, 0.05, "2017-11-18"));
      measurementRepository.save(needDataPoints);
  
      backgroundDataPoints.add(new MeasurementPoint(0.2, 0.4, 0.2, "2017-11-19"));
      backgroundDataPoints.add(new MeasurementPoint(0.4, 0.3, 0.15, "2017-11-12"));
      measurementRepository.save(backgroundDataPoints);
     //backgroundDataPoints.add(new MeasurementPoint(0.5, 0.2, 0.10, "2017-11-15"));
     //measurementRepository.save(backgroundDataPoints);
  </code></pre>
  
  <p>Here is the output from saving these points:</p>
  
  <pre><code>+-----+------------+----------+--------+
  | f1  | date       | precison | recall |
  +-----+------------+----------+--------+
  | 0.2 | 2017-11-19 |      0.4 |    0.2 |
  | 0.3 | 2017-11-19 |      0.5 |    0.2 |
  | 0.4 | 2017-11-12 |      0.3 |   0.15 |
  | 0.5 | 2017-11-15 |      0.3 |    0.1 |
  | 0.6 | 2017-11-18 |      0.2 |   0.05 |
  | 0.7 | 2017-11-12 |      0.4 |   0.15 |
  +-----+------------+----------+--------+
  6 rows in set (0.00 sec)
  </code></pre>
  
  <p>Here is my CrudRepository class: </p>
  
  <pre><code>public interface MeasurementRepository extends CrudRepository&lt;MeasurementPoint,Long&gt; {
  }
  </code></pre>
  
  <p><strong>SOLVED</strong></p>
  
  <p>saving these points works fine but as soon i save that out-commented new MeasurementPoint </p>
  
  <pre><code>//backgroundDataPoints.add(new MeasurementPoint(0.5, 0.2, 0.10, "2017-11-15"));
  //measurementRepository.save(backgroundDataPoints);
  </code></pre>
  
  <p>i get a error saying: I guess it has to do with duplicate values but how do i fix this?</p>
  
  <pre><code>javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session :
  </code></pre>
  
  <p>I solved it by creating a field of type long called id with annotation @Id because apparently every object created should get a unique id, the error thrown is because objects had some id. @GeneratedValue makes sure that everytime a new object is created a unqiue Id is given to that object.</p>
  
  <pre><code>    @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;
  </code></pre>