CrudRepositoryを使用してMYSQLデータベースにいくつかの値を保存しようとしていますが、うまくいかないようです。私は次のガイドhttps://spring.io/guides/gs/accessing-data-mysql/を試してみましたが、うまく動作しますが、私のプログラムで同じ原則に従おうとしているときに何らかの理由でうまく動作しないようです。ここMySQLのテーブルへの重複値を保存するJava Spring Hibernateがエラーを投げています
@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 | |
+----------+--------------+------+-----+---------+----------------+
はここ
テーブルが正しく作成される、ためのデータベースを作成するクラスIAMありますクラスiamはデータベースに値を追加しようとしています:
@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);
ここはoutpuですこれらの点を保存するからT:ここで
+-----+------------+----------+--------+
| 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を持っていたので、スローされたエラーがある長い注釈@IdとIDと呼ばれるタイプのフィールドを作成することによって、それを解決しました。 @GeneratedValueは、新しいオブジェクトが作成されるたびにそのオブジェクトにunqiue Idが与えられることを保証します。
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
IDを手動で設定すると、@GeneratedValueを使用しても問題はありません。Springデータはこのケースを処理する必要があります。Springデータ 'SimpleJpaRepository.save'メソッドにバグがあると思います。 3.0 –
@VitaliiMuzalevskyiの代わりに、crudrepositoryのデータ型をdoubleからintegerに変更することを意味しますか? – Genesis
いいえ、ちょうどあなたの値を変更する、私はいくつかの推測を持っています) –