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

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

常英资
2023-03-14

我在这里使用了多对一双向关系,一旦我被放入数据库,我就不能删除文件,如果我试图删除,我会遇到异常,无法删除或更新父行:外键约束失败。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.treamis.entity;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;

/**
 *
 * @author
 * admin
 */
@Entity
@Table(name = "LibraryBookListTable")
public class LibraryBookListEntity implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "BookListId")
    private int booklistid;
    @Column(name = "ISBN", nullable = false)
    private String isbn;
    @Column(name = "edition", nullable = false)
    private String edition;
    @Column(name = "publisher", nullable = false)
    private String publisher;
    @Column(name = "place", nullable = false)
    private String place;
    @Column(name = "page", nullable = false)
    private String page;
    @Column(name = "source", nullable = false)
    private String source;
    @Column(name = "billno", nullable = false)
    private String billno;
    @Column(name = "callno", nullable = false)
    private String callno;
    @Column(name = "BookTitle", nullable = false)
    private String booktitle;
    @Column(name = "BookAuthor", nullable = false)
    private String author;
    @Column(name = "BookPrice", nullable = false)
    private float price;
    @Column(name = "RackNumber", nullable = false)
    private String rack;
    @Column(name = "PublishedYear", nullable = false)
    private String publishedyear;
    @Column(name = "NoofCopies", nullable = false)
    private int tcopies;
    @Column(name = "DateAdded", nullable = false)
    private java.sql.Date dateAdded;
    @Column(name = "billdate", nullable = false)
    private java.sql.Date billdate;
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = CategoryEntity.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "category_Id", referencedColumnName = "category_Id", nullable = true)
    private CategoryEntity categoryid;
    @OneToOne
    private UserEntity addedBy;
    @OneToOne
    private UserEntity modifiedBy;
    @OneToMany(fetch = FetchType.LAZY, targetEntity = LibraryBarCodeEntity.class, cascade = CascadeType.ALL, mappedBy = "S_no")
    @JoinColumn(name = "BookListId", referencedColumnName = "BookListId")
    private Set< LibraryBarCodeEntity> chield;

    public Set<LibraryBarCodeEntity> getChield() {
        return chield;
    }

    public void setChield(Set<LibraryBarCodeEntity> chield) {
        this.chield = chield;
    }
    //@Column(name = "AddedDate", nullable = false)
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private java.util.Date addedate;
    // @Column(name = "ModifiedDate", nullable = false)
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private java.util.Date modifiedDate;

    public int getBooklistid() {
        return booklistid;
    }

    public void setBooklistid(int booklistid) {
        this.booklistid = booklistid;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getBooktitle() {
        return booktitle;
    }

    public void setBooktitle(String booktitle) {
        this.booktitle = booktitle;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getRack() {
        return rack;
    }

    public void setRack(String rack) {
        this.rack = rack;
    }

    public int getTcopies() {
        return tcopies;
    }

    public void setTcopies(int tcopies) {
        this.tcopies = tcopies;
    }

    public java.sql.Date getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded(java.sql.Date dateAdded) {
        this.dateAdded = dateAdded;
    }

    public CategoryEntity getCategoryid() {
        return categoryid;
    }

    public void setCategoryid(CategoryEntity categoryid) {
        this.categoryid = categoryid;
    }

    public UserEntity getAddedBy() {
        return addedBy;
    }

    public void setAddedBy(UserEntity addedBy) {
        this.addedBy = addedBy;
    }

    public UserEntity getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(UserEntity modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public java.util.Date getAddedate() {
        return addedate;
    }

    public void setAddedate(java.util.Date addedate) {
        this.addedate = addedate;
    }

    public java.util.Date getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(java.util.Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

//    public String getAccessionnumber() {
//        return accessionnumber;
//    }
//
//    public void setAccessionnumber(String accessionnumber) {
//        this.accessionnumber = accessionnumber;
//    }
    public String getEdition() {
        return edition;
    }

    public void setEdition(String edition) {
        this.edition = edition;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getBillno() {
        return billno;
    }

    public void setBillno(String billno) {
        this.billno = billno;
    }

    public String getCallno() {
        return callno;
    }

    public void setCallno(String callno) {
        this.callno = callno;
    }

    public java.sql.Date getBilldate() {
        return billdate;
    }

    public void setBilldate(java.sql.Date billdate) {
        this.billdate = billdate;
    }

    public String getPublishedyear() {
        return publishedyear;
    }

    public void setPublishedyear(String publishedyear) {
        this.publishedyear = publishedyear;
    }
//    public Set< LibraryBarCodeEntity> getChield() {
//        return chield;
//    }
//
//    public void setChield(Set< LibraryBarCodeEntity> chield) {
//        this.chield = chield;
//    }
}

这是我的另一个实体类...

package com.treamis.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *
 * @author
 * admin
 */
@Entity
@Table(name = "LibraryBarCodeTable")
public class LibraryBarCodeEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "S_no", nullable = false)
    private int S_no;
    @Column(name = "BookBarCode", nullable = false)
    private String barCode;
    private String accessno;
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = LibraryBookListEntity.class)
    @JoinColumn(name = "BookListId", referencedColumnName = "BookListId")
    private LibraryBookListEntity parent;

    public LibraryBookListEntity getParent() {
        return parent;
    }

    public void setParent(LibraryBookListEntity parent) {
        this.parent = parent;
    }

    public int getS_no() {
        return S_no;
    }

    public void setS_no(int S_no) {
        this.S_no = S_no;
    }

    public String getBarCode() {
        return barCode;
    }

    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }

    public String getAccessno() {
        return accessno;
    }

    public void setAccessno(String accessno) {
        this.accessno = accessno;
    }
}

嗨这是我的完整栈迹

2014-01-09 15:44:26 ERROR JDBCExceptionReporter:78 - Cannot delete or update a parent row: a foreign key constraint fails (`treamisdemo`.`librarybooklisttable`, CONSTRAINT `FK33BC700C2F4991AA` FOREIGN KEY (`category_Id`) REFERENCES `categoryentity` (`category_Id`))
2014-01-09 15:44:26 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at com.treamis.library.BookList.LibraryBookListDelete.execute(LibraryBookListDelete.java:90)
    at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)

共有1个答案

龙玺
2023-03-14

我不知道这会有什么帮助,但是如果你还没有这样做,你可以编辑你的hibernate。cfg。xml文件并设置hibernate。将sql从false显示为true,以便查看异常发生时生成的sql语句。检查它们可能会为你提供一条关于发生了什么的线索。您还可以考虑将打印语句(Soal.Out.PrtLn)放入SET*()函数中,以查看主键和外键值是什么,这样您就可以检查数据库中的数据(现在您知道主键值,您可以看到正在处理什么记录)。br>
示例:系统。出来println(“在MyDaoClass中:调用客户表:setId()=”客户。getID());

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

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

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

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

  • 我正在做一个基本任务来移除级联任务,但我得到了一个例外。 类与类和类之间存在关系。 我有以下实体: 任务。JAVA 响应CheckLists.java 统计调查。JAVA 当我想删除从MySQL数据库如下(API REST): 然后我得到以下错误 无法删除或更新父行:外键约束失败(,约束外键()引用() hibernate查询: 编辑1: 我也有同样的问题 编辑2: 如何删除其及其子项? 非常感谢

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