JPA / Hibernate:On删除外键约束失败

时间:2016-09-06 15:58:09

标签: java hibernate jpa spring-data

我正在尝试在数据库中设计OneToMany关系,以便于删除数据。但面临一些错误。我有两个实体:

@Entity
@Table(name="ALGOS")
public class Algo extends Base{

    @Fetch(FetchMode.SELECT)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "algo", orphanRemoval = true, fetch = FetchType.EAGER)
    @JsonIgnore private List<Evals> evals=new ArrayList();

    @Fetch(FetchMode.SELECT)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "algo", orphanRemoval = true, fetch = FetchType.EAGER)
    @JsonIgnore private List<Recs> recs=new ArrayList();

    @JsonIgnore
    public List<Evals> getEvals(){
        return evals;
    }
    @JsonIgnore
    public List<Recs> getRecs(){
        return recs;
    }
    }

@Entity
@Table(name = "RECS_T")
public class Recs extends Base{
    @ManyToOne
    @JoinColumn(name = "ALGO_ID")
    private Algo algo;

    @ManyToOne
    @JoinColumn(name = "EVAL_ID")
    private Evals eval;

    @ManyToOne
    @JoinColumn(name = "CONF_ID")
    private Confs conf;

    @Fetch(FetchMode.SELECT)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "recid", orphanRemoval = true, fetch = FetchType.EAGER)
    @JsonIgnore
    private List<RecPubs> recpubs=new ArrayList();

    @JsonIgnore
    public List<RecPubs> getRecPubs(){
        return recpubs;
    }
}

Algo是父表和Recs - child是值得注意的。所以,我正在做的是尝试从Algo表中删除一个实体,只做algoRepository.delete(algo),但我收到一个错误:

ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot delete or update a parent row: a foreign key constraint fails (`db`.`recs`, CONSTRAINT `FK9ux9oqx6yr66cva4ro6l8m63r` FOREIGN KEY (`ALGO_ID`) REFERENCES `algo` (`ALGO_ID`))
ERROR: org.hibernate.internal.ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
ERROR: com.app.controller.AdminController - Exception during deleting algorithm due to  could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

任何想法,我该如何解决这个问题?

0 个答案:

没有答案