【发布时间】:2016-08-28 00:18:45
【问题描述】:
我有 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 但我仍然得到同样的错误。
此外,当您有外键记录时,授予用户删除类别的权限是否正确?如果是的话,在这个例子中怎么做
谢谢
【问题讨论】:
-
当您说您尝试了 cascade.all 时,您是否将其添加到类别或出版物中?
-
我在发布中添加了级联,我在 Category 中没有发布实例,所以我没有在那里添加它。我在发布中添加了级联
标签: hibernate spring-mvc mapping