DataIntegrityViolationException:无法执行语句; SQL不适用;约束fk_qjc1iwbdvgwixafwdkjn82tmt

时间:2019-03-03 06:38:16

标签: java hibernate spring-mvc jpa annotations

我试图弄清楚休眠。我以为我对所有的互联网教程都还不错,但是我对这个例外一无所知。我正在尝试将包含几个列表的对象保存到数据库中。 该对象称为DataPointsListResultSet,它包含一个List预测数据点列表和一个实际数据点列表

以下是模型:

(DataPointsListResultSet)

@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;
}

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

1 个答案:

答案 0 :(得分:0)

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