【发布时间】:2017-11-14 09:52:29
【问题描述】:
我正在尝试使用 CrudRepository 将一些值保存到 MYSQL 数据库,但似乎不起作用。我已经尝试了以下指南https://spring.io/guides/gs/accessing-data-mysql/,它工作正常,但是当我试图对我的程序遵循相同的原则时,它似乎由于某种原因不起作用。
这是创建数据库的类,该表已正确创建:
@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;
}
}
这里是 MSQL 终端为创建的实体输出列:
+----------+--------------+------+-----+---------+----------------+
| 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 | |
+----------+--------------+------+-----+---------+----------------+
这是我试图从以下位置向数据库添加值的类:
@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);
这是保存这些点的输出:
+-----+------------+----------+--------+
| 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)
这是我的 CrudRepository 类:
public interface MeasurementRepository extends CrudRepository<MeasurementPoint,Long> {
}
已解决
保存这些点可以正常工作,但一旦我保存了注释掉的新 MeasurementPoint
//backgroundDataPoints.add(new MeasurementPoint(0.5, 0.2, 0.10, "2017-11-15"));
//measurementRepository.save(backgroundDataPoints);
我收到一条错误消息:我猜这与重复值有关,但我该如何解决?
javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session :
我通过创建一个名为 id 并带有注释 @Id 的 long 类型字段来解决它,因为显然每个创建的对象都应该有一个唯一的 id,抛出的错误是因为对象有一些 id。 @GeneratedValue 确保每次创建新对象时都会为该对象提供一个唯一的 Id。
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
【问题讨论】:
标签: java mysql database spring hibernate