首页 > 解决方案 > 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;
}

任何人都可以对这个问题有所了解,我真的很感激!

标签: javahibernatespring-mvcjpaannotations

解决方案


在您的情况下,您可以尝试cascade = CascadeType.ALL从您DataPointsListResultSet entity的实体中手动删除并保存两个实体的操作。


推荐阅读