当前位置: 首页 > 知识库问答 >
问题:

Hibernate抛出-无法删除或更新父行:外键约束失败

宋飞文
2023-03-14

我正在做一个基本任务来移除级联任务,但我得到了一个例外。

任务与类响应检查列表和类统计任务之间存在OneToMany关系。

我有以下实体:

任务。JAVA

@Entity
public class Missions implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="idMission")
private long idMission;

@Column(name="nomMission")
private String nomMission;

@OneToMany(mappedBy="Respmission", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Collection <ResponsesCheckLists> ResponsesCheckLists;
//getters and setters

 @JsonIgnore
public Collection<ResponsesCheckLists> getResponsesCheckLists() {
    return ResponsesCheckLists;
}

public void setResponsesCheckLists(Collection<ResponsesCheckLists> responsesCheckLists) {
    ResponsesCheckLists = responsesCheckLists;
   }
}

响应CheckLists.java

@Entity
public class ResponsesCheckLists implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="idResponsesCHeck")
    private long idResponsesCHeck;

    @ManyToOne
    @JoinColumn(name="missionsId")
    private Missions Respmission;

    @ManyToOne
    @JoinColumn(name="checkLists_Id")
    private CheckLists CheckLts;
     //getters and setters

     public Missions getRespmission() {
        return Respmission;
    }

    public void setRespmission(Missions respmission) {
        Respmission = respmission;
    }

    public CheckLists getCheckLts() {
        return CheckLts;
    }

    public void setCheckLts(CheckLists checkLts) {
        CheckLts = checkLts;
    }

}

统计调查。JAVA

@Entity
public class StatistisquesMessions implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="idStat")
    private long idStat;

    //@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @ManyToOne
    @JoinColumn(name="equipement_ids")
    @JsonBackReference
    private Equipements statEquipements;

    @ManyToOne
    @JoinColumn(name="statMission_id")
    @JsonBackReference
    private Missions statMissions;
}

当我想删除任务从MySQL数据库如下(API REST):

@Override
    public Boolean DeleteT(Pk id) {

        T tt= GR.findOne(id);
        GR.delete(tt);
        return true;
    }
@Autowired
    private MissionsMetier missionM;

@RequestMapping(value="/DeleteMissions")
    public Missions DeleteMissions(@Param(value ="idMission") long idMission){
        Missions mis = missionM.getT(idMission);     //I have the object "Missions"
        return missionM.DeleteT(idMission);
        }

然后我得到以下错误

无法删除或更新父行:外键约束失败(BDTasksresponses\u check\u list,约束FK73o6po4goa6ulera1d6md58i6外键(missions\u id)引用missionsid\u missions

hibernate查询:

Hibernate: select stat0_.stat_mission_id as stat_mis8_9_0_, stat0_.id_stat as id_stat1_9_0_, stat0_.id_stat as id_stat1_9_1_, stat0_.nbre_conforme as nbre_con2_9_1_, stat0_.nbre_not_conforme as nbre_not3_9_1_, stat0_.res_conforme as res_conf4_9_1_, stat0_.res_not_conforme as res_not_5_9_1_, stat0_.equipement_ids as equipeme7_9_1_, stat0_.stat_mission_id as stat_mis8_9_1_, stat0_.total_check_lists as total_ch6_9_1_, equipement1_.id_equipements as id_equip1_3_2_, equipement1_.actifs_id as actifs_i5_3_2_, equipement1_.date_ajout_eq as date_ajo2_3_2_, equipement1_.date_modification_eq as date_mod3_3_2_, equipement1_.nom_eq as nom_eq4_3_2_, actifs2_.id_actif as id_actif1_0_3_, actifs2_.date_ajout_actif as date_ajo2_0_3_, actifs2_.nom_actif as nom_acti3_0_3_ from statistisques_messions stat0_ left outer join equipements equipement1_ on stat0_.equipement_ids=equipement1_.id_equipements left outer join actifs actifs2_ on equipement1_.actifs_id=actifs2_.id_actif where stat0_.stat_mission_id=?
Hibernate: delete from statistisques_messions where id_stat=?
Hibernate: delete from missions where id_mission=?

编辑1:

@Autowired
    private MissionsMetier missionM;

    @RequestMapping(value="/DeleteMissions")
    public Missions DeleteMissions(@Param(value ="idMission") long idMission){
        Missions mis = missionM.getT(idMission);     //I have the object "Missions"
          // delete ResponsesCheckLists
    Collection<ResponsesCheckLists> Rp = res.FinsRespoCHeckListsMission(idMission);
    for(ResponsesCheckLists r : Rp){
        System.out.println("***** id response "+r.getIdResponsesCHeck());
         res.DeleteT(r.getIdResponsesCHeck());
     }

     //delete StatistisquesMessions
     Collection<StatistisquesMessions> st =mis.getStat(); 
     for(StatistisquesMessions stat : st){
              statis.DeleteT(stat.getIdStat());
          }

    mis.removeMissionsFromUsers();
   // return  missionM.DeleteT(idMission);
    //return true;
    return missionM.DeleteT(idMission); 

   return  missionM.DeleteT(idMission);

        }

我也有同样的问题

编辑2:

@Autowired
    private MissionsMetier missionM;

    @RequestMapping(value="/DeleteMissions")
    public Missions DeleteMissions(@Param(value ="idMission") long idMission){
        Missions mis = missionM.getT(idMission);     //I have the object "Missions"

        // delete ResponsesCheckLists
    Collection<ResponsesCheckLists> Rp = res.FinsRespoCHeckListsMission(idMission);
    for(ResponsesCheckLists r : Rp){
        System.out.println("***** id response "+r.getIdResponsesCHeck());
         res.DeleteT(r.getIdResponsesCHeck());
     }

     //delete StatistisquesMessions
     Collection<StatistisquesMessions> st =mis.getStat(); 
     for(StatistisquesMessions stat : st){
              statis.DeleteT(stat.getIdStat());
          }

    mis.removeMissionsFromUsers();

       return  missionM.DeleteT(mis.getIdMission());
        }

如何删除任务响应检查列表及其统计任务子项?

非常感谢你,

共有1个答案

连正信
2023-03-14

此问题通过以下代码解决:

@RequestMapping(value="/DeleteMissions")
    public Missions DeleteMissions(@Param(value ="idMission") long idMission){
        Missions mis = missionM.getT(idMission);     //I have the object "Missions"

        // delete ResponsesCheckLists
        Collection<ResponsesCheckLists> Rp = res.FinsRespoCHeckListsMission(idMission);
        for(ResponsesCheckLists r : Rp){
                r.getCheckLts().setResponsesChecks(null);
                r.setCheckLts(null);
               res.saveT(r);
               res.DeleteT(r.getIdResponsesCHeck());
         }

         //delete StatistisquesMessions
         Collection<StatistisquesMessions> st =mis.getStat(); 
         for(StatistisquesMessions stat : st){
                  statis.DeleteT(stat.getIdStat());
        }   

       return  missionM.DeleteT(idMission);
        }
 类似资料:
  • 我还在学习hibernate中的很多东西,以及如何处理hibernate中表与表之间的关系,所以在我的一个项目中,我面临着以下问题: 问题出在哪里? 我想做什么? 我正在尝试删除package实体,但不删除package表中引用的用户 我正在通过packageDAO对package实体调用delete

  • 我在这里使用了多对一双向关系,一旦我被放入数据库,我就不能删除文件,如果我试图删除,我会遇到异常,无法删除或更新父行:外键约束失败。 这是我的另一个实体类... 嗨这是我的完整栈迹

  • 问题内容: 进行时: 错误: 这是我的桌子: 问题答案: 照原样,必须先删除Advertisers表中的行,然后才能删除它引用的Jobs表中的行。这个: …实际上与应有的相反。实际上,这意味着您必须在作业表中有一条记录,然后才是广告商。因此,您需要使用: 纠正外键关系后,您的delete语句将起作用。

  • 问题内容: 我正在研究一个基本示例来测试操作,但出现异常。 我有以下实体: Employee.java EmpDetails.java 现在我在数据库中有员工ID为10的记录,在员工详细信息表中有相应的记录。 现在,当我在查询下面运行时: 我在想hibernate将删除员工记录和相应的员工详细信息记录,因为我已设置要删除的层叠类型。但我得到异常为: 引起原因:com.mysql.jdbc.exce

  • 我正在处理一个测试操作的基本示例,但我得到了异常。 我有以下实体: 有人能帮助我如何测试级联删除选项在这里吗?

  • 我将按id删除对象,但出现如下错误: com。mysql。jdbc。例外情况。jdbc4。MySQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败(,constraint外键()引用) 我按id删除的方法是: 我的表的映射看起来像: Selection.java 作业Audit.java 审核组。JAVA AssignmentAudit