当我尝试在我的应用程序中创建一个新类别时,我收到以下错误,这是抱怨FK约束。然而,FK抱怨将类别描述映射到类别,因为两者都在同时创建,我不知道为什么会出现问题。
我可以毫无问题地更新类别和类别说明。
错误
ERROR - Cannot add or update a child row: a foreign key constraint fails (`crcart3`.`categorydescription`, CONSTRAINT `FKC6C4CBFE6FD28044` FOREIGN KEY (`category_description_id`) REFERENCES `categorydescription` (`category_id`))
类别表
@Table(name = "Category")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private int categoryId;
@JoinTable(
name = "ImageMapping",
joinColumns = @JoinColumn(name = "category_id"),
inverseJoinColumns = @JoinColumn(name = "image_id")
)
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Image categoryImageId;
@JoinTable(
name="CategoryDescription",
joinColumns = @JoinColumn(name="category_id"),
inverseJoinColumns = @JoinColumn(name="category_description_id")
)
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private CategoryDescription categoryDescription;
@JoinTable(
name = "CategoryMapping",
joinColumns = @JoinColumn(name = "category_id"),
inverseJoinColumns = @JoinColumn(name = "parent_category_id")
)
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Category parentCategory;
@Column(name = "category_sort_order")
private int sortOrder;
@Column(name = "date_added")
private Timestamp dateAdded;
@Column(name = "last_modified")
private Timestamp lastModified;
类别说明表
@Table(name="CategoryDescription")
public class CategoryDescription {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_description_id")
private int categoryDescriptionId;
@Column(name="category_id")
private int categoryId;
@OneToOne
@JoinColumn(name="language_id")
private Language languageId;
@Column(name="category_name")
private String categoryName;
@Column(name="category_text")
private String categoryText;
答案 0 :(得分:1)
错误消息显示:
(`crcart3`.`categorydescription`,
CONSTRAINT `FKC6C4CBFE6FD28044` FOREIGN KEY (`category_description_id`)
REFERENCES `categorydescription` (`category_id`))
这意味着在表categorydescription
中,列category_description_id
是相同表category_id
的列categorydescription
的外键
这对我来说不合适。在这个表中有一个category_id
列似乎是不对的。