我正在尝试使用 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。 @GenerateValue 确保每次创建新对象时都会为该对象提供一个唯一的 Id。
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
最佳答案
我认为由于错误而未保存数据。您只需要检查日志即可。启用Hibernate SQL logs并检查那里的insert
语句。
您将
f1
作为自动生成的列,但为其设置了值。另外,我不确定是否可以自动生成double
列。Hibernate 需要一个默认构造函数。
MeasurementPoint
没有。
这意味着java.lang.NullPointerException: null(DataChart.java:30)
Spring 没有设置measurementRepository 字段。尝试删除 static
修饰符。
此org.hibernate.PersistentObjectException:分离的实体传递给持久:
意味着您不应使用save
方法指定f1
。
关于Java Spring Hibernate 将重复值保存到 MySQL 表会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47282518/