java - 我的子表没有删除

标签 java hibernate

这是我的实体文件。在这里我使用了双向映射,我的要求是,如果我删除父项,我想同时删除父项和子项,如果我删除子项,我只想删除子项而不是父项。但这里不起作用,任何人都可以解释我如何做到这一点。

/*
 * 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.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;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 *
 * @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)
    @JoinColumn(name = "category_Id", referencedColumnName = "category_Id", nullable = true)
    private CategoryEntity categoryid;
    @OneToOne
    private UserEntity addedBy;
    @OneToOne
    private UserEntity modifiedBy;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
    @JoinColumn(name = "BookListId", referencedColumnName = "BookListId")
    @Cascade({CascadeType.SAVE_UPDATE})
    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;
//    }
}

最佳答案

更改表,将其设置为 DELETE CASCADE。

在 hibernate 状态下,这就像

<set name="stockDailyRecords" cascade="delete" table="stock_daily_record" ...>
      <key>
            <column name="STOCK_ID" not-null="true" />
      </key>
      <one-to-many class="com.mkyong.common.StockDailyRecord" />
</set>

看到这个link

关于java - 我的子表没有删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21251836/

相关文章:

java - 在 hibernate 中过滤计算列? (拥有)

java - 未设置 'hibernate.dialect' 时连接不能为空

hibernate - c3p0 死锁检测 - 线程运行过于频繁

java - 如何从我的字符串中一一检索值

java - 如何重写 boolean 值handleUnattended()

java - Spring 启动 : Using a @Service in Quartz job execution

java - 使用 Spring Boot Hibernate Criteria API PostGRES 获取查询的部分输入结果

java - 异常 : Session Issue ids for this class must be manually assigned before calling save(): org. me.Testservices.TblUsers

java - 浏览器插件 | COM 端口通信技术

java - 在Java中包含空格的工作目录中执行外部可执行文件?