簡體   English   中英

HIbernate無法使用外鍵刪除實體。 外鍵設置為null

[英]HIbernate Can't delete Entity with foreign key. Foreign key gets set to 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<ItemCategory> 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 o.h.e.jdbc.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<ItemCategory> 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(這是我從您的異常中觀察到的)。 在大多數情況下,它不是優選的。 我們通常讓孩子們擁有這種關系。 這是通過使用mappedBy完成的

(偽代碼)

class Item {
  //...

  @OneToMany(mappedBy = "item", cascade=ALL, orphanRemoval=true)
  private Set<ItemCategory> categories;
}

class ItemCategory {
  //...

  @ManyToOne
  @JoinColumn(name="item_id")
  Item item;
}

這里的技巧是mappedBy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM