I am trying to save some values to a MYSQL database using CrudRepository but doesnt seem to work. I have tried the following guide https://spring.io/guides/gs/accessing-data-mysql/ and it works fine but when iam trying to follow the same principle for my program it doesnt seem to work for some reason.
Here is the class iam creating a database for, The table gets created properly:
@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;
}
}
Here is the MSQL terminal outputting the columns for the entity created:
+----------+--------------+------+-----+---------+----------------+
| 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 | |
+----------+--------------+------+-----+---------+----------------+
Here is the class iam trying to add values to the database from:
@Controller
public class DataChart {
private static final Logger logger= LoggerFactory.getLogger(DataChart.class);
@Autowired
private static MeasurementRepository measurementRepository;
@GetMapping(value = "/statementchart")
@ResponseBody
public List<Measurement> scoreChartData() {
List<MeasurementPoint> needDataPoints = new ArrayList<>();
List<MeasurementPoint> backgroundDataPoints = new ArrayList<>();
List<MeasurementPoint> goalDataPoints=new ArrayList<>();
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);
Here is the output from saving these points:
+-----+------------+----------+--------+
| 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)
Here is my CrudRepository class:
public interface MeasurementRepository extends CrudRepository<MeasurementPoint,Long> {
}
SOLVED
saving these points works fine but as soon i save that out-commented new MeasurementPoint
//backgroundDataPoints.add(new MeasurementPoint(0.5, 0.2, 0.10, "2017-11-15"));
//measurementRepository.save(backgroundDataPoints);
i get a error saying: I guess it has to do with duplicate values but how do i fix this?
javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session :
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.
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;