HIbernate无法使用外键删除实体。 外键设置为null

这个问题在很多方面都有问题,但没有一个解决方案对我有用。 我正在尝试删除父实体,我希望也删除所有子实体。

我的实体:

@Entity @Table(name = "item", catalog = "myshchema") public class Item implements java.io.Serializable { @JoinColumn(name = "item_id", insertable = false, updatable = false, nullable = false) @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) private Set categories; /* Getters and Setters and other fields*/ } 

物品表:

 CREATE TABLE `item` ( `item_id` int(11) NOT NULL AUTO_INCREMENT, `store_id` int(11) NOT NULL, PRIMARY KEY (`item_id`), UNIQUE KEY `item_id_UNIQUE` (`item_id`), KEY `FK_ITEM_STORE_ID_idx` (`store_id`), CONSTRAINT `FK_ITEM_STORE_ID` FOREIGN KEY (`store_id`) REFERENCES `store` (`store_id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=84 DEFAULT CHARSET=utf8; 

而我的另一个实体

 @Entity @Table(name = "item_category", catalog = "myschema") @IdClass(ItemCategoryIndex.class) public class ItemCategory implements java.io.Serializable { @Id @Column(name = "category_id", unique = true, nullable = false, insertable = false, updatable = false) private Integer categoryId; @Id private Store store; @Id private Item item; @Id private String categoryName; /* Getters and Setters */ } 

ItemCategory表:

 CREATE TABLE `item_category` ( `category_id` int(11) NOT NULL AUTO_INCREMENT, `store_id` int(11) NOT NULL, `item_id` int(11) NOT NULL, `category_name` varchar(45) NOT NULL, PRIMARY KEY (`category_id`), UNIQUE KEY `category_id_UNIQUE` (`category_id`), UNIQUE KEY `IDX_UNIQUE_STORE_CATEGORY` (`store_id`,`item_id`,`category_name`) USING BTREE, KEY `FK_CATEGORY_STORE_ID_idx` (`store_id`), KEY `FK_ITEM_CATEGORY_ID_idx` (`item_id`), CONSTRAINT `FK_CATEGORY_STORE_ID` FOREIGN KEY (`store_id`) REFERENCES `store` (`store_id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `FK_ITEM_CATEGORY_ID` FOREIGN KEY (`item_id`) REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE NO ACTION ) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=utf8; 

我尝试删除这样的项目:

 Item item = entityManager.find(Item.class, idList.get(i)); entityManager.remove(item); 

我的日志显示Hibernate正在尝试将ItemCategory的主键设置为null:

 Hibernate: update myschema.item_category set item_id=null where item_id=? ERROR ohejdbc.spi.SqlExceptionHelper.logExceptions 146 - Column 'item_id' cannot be null 

我甚至尝试循环遍历子记录并手动删除它们,但Hibernate仍然将此更新发送到null查询。 我究竟做错了什么?

我必须将你的问题分解为两部分

首先 – 让我们谈谈您的数据库架构设计。

根据您的架构, itemitem_category具有一对多的关系,这意味着一个项目可以/被分配给不同的类别,但不同的项目不能/被分配到同一类别。

如果它确实是你的业务需求 ,那就完全没问了,我提到它是因为它对我没有意义,这种情况很少发生。

如果你想要的是一个类别可以有多个项目,反之亦然, itemitem_category必须是多对多关系。 还应该有一个连接表

第二 – 让我们说架构不会改变

ItemCategory是关系的所有者,因为它具有引用item表的外键item_id 。 所以ItemCategoy看起来应该大致如下:

 @Entity @Table(name = "item_category") public class ItemCategory { @Id private Integer categoryId; private Store store; @ManyToOne @JoinColumn(name="item_id", /*cascade = ...*/) private Item item; private String categoryName; /* Getters and Setters */ } 

您的Item实体大致如下:

 @Entity @Table(name = "item", catalog = "myshchema") public class Item implements java.io.Serializable { @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy="item") private Set categories; //`mappedBy`used here because this entity is not the owner of the relationship according to what mentioned above /* Getters and Setters and other fields*/ } 

要简单地从Item删除所有子实体( ItemCategory

em.remove(item);

orphanRemovaltrue ,删除父级,子级也将被删除。

在Hibernate中,您需要决定谁拥有这种关系。 如果您拥有该关系的父方(ItemCategory),您会发现Item + ItemCategory的插入/删除将涉及ItemCategory表中的item_id的更新(这是我从您的exception中观察到的)。 在大多数情况下,它不是优选的。 我们通常让孩子们拥有这种关系。 这是通过使用mappedBy完成的

(伪代码)

 class Item { //... @OneToMany(mappedBy = "item", cascade=ALL, orphanRemoval=true) private Set categories; } class ItemCategory { //... @ManyToOne @JoinColumn(name="item_id") Item item; } 

这里的技巧是mappedBy