Hibernate,存在外键约束时删除父行

时间:2016-08-28 00:18:46

标签: hibernate spring-mvc mapping

我有2个实体类别和出版物。我在这里的关系是每个出版物都有一个类别。

像这样的东西

create table category(
id INTEGER not null auto_increment,
name varchar(255) not null,
primary key (id)
);


CREATE TABLE PUBLICATION(
ID INTEGER NOT NULL AUTO_INCREMENT,
TITLE varchar(255) NOT NULL,
CONTENT VARCHAR(1000) NOT NULL,
CATEGORY_ID INTEGER NOT NULL,
primary key(ID),
foreign key (CATEGORY_ID) references CATEGORY (ID)
);

和实体对象

package com.java.bricks.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;



    @Entity
    @Table(name="PUBLICATION")
    public class PublicationEntity implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Id
        @Column(name="ID")
        @GeneratedValue(strategy=GenerationType.AUTO)
        private Long publicationId;

        @Column(name="TITLE")
        private String title;

        @Column(name="CONTENT")
        private String content;


        @OneToOne
        @JoinColumn(name="CATEGORY_ID")
        private CategoryEntity category;


        public Long getPublicationId() {
            return publicationId;
        }
        public void setPublicationId(Long publicationId) {
            this.publicationId = publicationId;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public CategoryEntity getCategory() {
            return category;
        }
        public void setCategory(CategoryEntity category) {
            this.category = category;
        }



    }

和类别实体

package com.java.bricks.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="CATEGORY")
public class CategoryEntity implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long categoryId;

    @Column(name="NAME")
    public String categoryName;


    public Long getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }



}

现在,当我删除发布记录时,发布记录将被成功删除。这很好用。但现在我想要的是,当我删除类别记录时,它应该删除发布表中的类别记录(父)和所有外键记录。

我该怎么做?我做了cascade.all,但我仍然得到同样的错误。

此外,当您拥有外键记录时,是否允许访问用户以删除该类别?如果是,在这个例子中怎么做呢

感谢

1 个答案:

答案 0 :(得分:1)

Hibernate通常只会查看被删除的类而不是扫描整个实体图。如果该类中没有明确的链接,那么它将不会执行任何操作。由于Category没有FK,因此您可以在注释中使用mappedBy属性,它将查找具有该名称的实例变量。当它找到实例变量时,它将能够推断出实体之间的关系。

将级联视为从此实体到映射实体的操作流程。删除类别实体时,它会查看发布映射,并看到删除应该级联到该发布。

package com.java.bricks.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="CATEGORY")
public class CategoryEntity implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long categoryId;

    @Column(name="NAME")
    public String categoryName;

    @OneToOne(mappedBy="category", cascade = CascadeType.ALL)
    private Publication publication;    

    public Long getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }



}