簡體   English   中英

休眠:無法刪除或更新父行:外鍵約束失敗

[英]Hibernate :Cannot delete or update a parent row: a foreign key constraint fails

我在這里已經使用了多對一的雙向關系,一旦嘗試刪除就無法刪除文件,如果我要刪除,則會遇到異常,無法刪除或更新父行:外鍵約束失敗。

/*
 * 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)

我不知道這會有所幫助,但是如果您尚未這樣做,則可以編輯hibernate.cfg.xml文件並將hibernate.show_sql從false設置為true,以查看在發生異常。 檢查它們可能會為您提供提示。 您還可以考慮將print語句(System.out.println)放在set *()函數中,以查看主鍵和外鍵值是什么,以便您可以檢查數據庫中的數據(現在您知道主鍵值了,可以查看正在處理的記錄)。

示例:MyDaoClass中的System.out.println(“:調用客戶表:setId()=” + customer.getID());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM