java - DataIntegrityViolationException:无法执行语句;SQL 不适用;约束 fk_qjc1iwbdvgwixafwdkjn82tmt
问题描述
我试图弄清楚休眠。我以为我对所有互联网教程都做得很好,但我被这个例外困住了。我正在尝试将包含几个列表的对象保存到我的数据库中。该对象称为 DataPointsListResultSet,它包含一个 List predictDataPointsList 和一个 actualDataPointsList
以下是模型:
(数据点列表结果集)
@Entity
public class DataPointsListResultSet {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer resultsetid;
@OneToMany(targetEntity= DataPoints.class,
mappedBy="dataPointsid",cascade = CascadeType.ALL,
fetch=FetchType.EAGER)
private List<DataPoints> predictDataPointsList = new ArrayList<>();
@OneToMany(targetEntity= DataPoints.class, mappedBy="dataPointsid",
cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<DataPoints> actualDataPointsList = new ArrayList<>();
//getters and setters
(数据点)
@Entity
public class DataPoints {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer dataPointsid;
double x;
double y;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "resultsetid")
DataPointsListResultSet dataPointsListResultSet;
public DataPoints(){
}
//getters and setters
以下是一些 Stacktrace:
Message Request processing failed; nested exception is
org.springframework.dao.DataIntegrityViolationException: could not
execute statement; SQL [n/a]; constraint
[fk_337ty7afmhvcde8gwkd0sd6bq]; nested exception is
org.hibernate.exception.ConstraintViolationException: could not
execute statement
Description The server encountered an unexpected condition that
prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is
org.springframework.dao.DataIntegrityViolationException: could not
execute statement; SQL [n/a]; constraint
[fk_337ty7afmhvcde8gwkd0sd6bq]; nested exception is
org.hibernate.exception.ConstraintViolationException: could not
execute statement
Root Cause
org.springframework.dao.DataIntegrityViolationException: could not
execute statement; SQL [n/a]; constraint
[fk_337ty7afmhvcde8gwkd0sd6bq]; nested exception is
org.hibernate.exception.ConstraintViolationException: could not
execute statement
这是执行逻辑的 Java 方法:
public DataPointsListResultSet predictPriceOneAhead (MultiLayerNetwork net, List<Pair<INDArray, INDArray>> testData, double max, double min, int exampleLength, String nomeDoConjunto, GeneralStockDataSetIterator iterator) {
double[] predicts = new double[testData.size()];
double[] actuals = new double[testData.size()];
DataPointsListResultSet resultSet = new DataPointsListResultSet();
List<DataPoints> predictDataPointsList = new ArrayList<>();
List<DataPoints> actualDataPointsList = new ArrayList<>();
resultSet.setPredictDataPointsList(predictDataPointsList);
resultSet.setActualDataPointsList(actualDataPointsList);
resultSetDao.save(resultSet);
for (int i = 0; i < testData.size(); i++) {
predicts[i] = net.rnnTimeStep(testData.get(i).getKey()).getDouble(exampleLength - 1) * (max - min) + min;
actuals[i] = testData.get(i).getValue().getDouble(0);
DataPoints predictDataPoint = new DataPoints();
predictDataPoint.setDataPointsListResultSet(resultSet);
predictDataPoint.setY(predicts[i]);
predictDataPoint.setX(i);
dataPointsDao.save(predictDataPoint);
predictDataPointsList.add(predictDataPoint);
DataPoints actuaDataPoint = new DataPoints();
actuaDataPoint.setDataPointsListResultSet(resultSet);
actuaDataPoint.setY(actuals[i]);
actuaDataPoint.setX(i);
dataPointsDao.save(actuaDataPoint);
actualDataPointsList.add(actuaDataPoint);
}
log.info("Print out Predictions and Actual Values...");
log.info("Predict,Actual");
for (int i = 0; i < predicts.length; i++) log.info(predicts[i] + "," + actuals[i]);
return resultSet;
}
任何人都可以对这个问题有所了解,我真的很感激!
解决方案
在您的情况下,您可以尝试cascade = CascadeType.ALL
从您DataPointsListResultSet
entity
的实体中手动删除并保存两个实体的操作。
推荐阅读
- javascript - 如何在 Javascript 中访问 ExchangeUser() 对象
- python - 如何使用多处理来更快地运行一个函数?
- matlab - 命名工作区变量并将其保存到名称中更改数字的文件中 - Matlab
- python - 为什么算法不接受浮点数?
- python - [[WinError 2]] 系统找不到指定的文件:'desktop.ini'
- javascript - Firebase 功能不适用于 axios
- amazon-s3 - Terraform 403 执行 terraform 计划时出现禁止错误
- java - 如何处理 Lucene 中的标识符字段?
- reactjs - GraphQL 中的嵌套排序
- ios - 使用结构时不断收到“无法赋值:'calculateBMI' 是一种方法”