【发布时间】:2019-07-24 18:17:17
【问题描述】:
我正在尝试找出 Hibernate。我以为我对所有互联网教程都做得很好,但我被这个例外困住了。我正在尝试将包含几个列表的对象保存到我的数据库中。 该对象称为 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;
}
任何人都可以对这个问题有所了解,我真的很感激!
【问题讨论】:
-
你能读到 db,
fk_337ty7afmhvcde8gwkd0sd6bq在哪里,它引用了什么? -
@Andronicus 我认为 fk 代表外键,但我的数据库中没有 337ty7afmhvcde8gwkd0sd6bq。它实际上正确创建了 DataPoints,但 DataPointsListResultSet 出现在数据库中,没有 List 对象,只有 id。
标签: java hibernate spring-mvc jpa annotations