HIbernate Can't delete Entity with foreign key. Foreign key gets set to null

后端 未结 2 1885
终归单人心
终归单人心 2021-02-15 06:08

This question has been asked in many forms here but none of the solutions seem to work for me. I\'m trying to delete the parent entity and I want all of the child entities to al

相关标签:
2条回答
  • 2021-02-15 06:54

    I have to break your problem down to two parts

    First - let's talk about your database schema design.

    According to your schema, item and item_category has a one-to-many relationship meaning an item can have/be-assigned-to different categories but different items cannot have/be-assigned-to the same category.

    That is totally fine if it is indeed your business requirement, I mention it because it does not make sense to me and this circumstance rarely happens.

    If what you want is that a category can have multiple items and vice versa, itemand item_category must be a many-to-many relationship. There should be a join table additionally.

    Second - let's say the schema don't change

    ItemCategory is the owner of the relationship because it has a foreign key item_id refering to item table. So the ItemCategoy should look roughly like this:

    @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 */
    }
    

    Your Item entity will be roughly like this:

    @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*/
    }  
    

    To remove all the child entities(ItemCategory) from Item , simply

    em.remove(item);

    The orphanRemoval is true, deleting the parent, the children will be deleted as well.

    0 讨论(0)
  • 2021-02-15 06:54

    In Hibernate, you need to decide who is owning the relationship. If you have the parent side (ItemCategory) owning the relationship, you will find insertion/deletion of Item+ ItemCategory will involve update of item_id in ItemCategory table (which is what I observed from your exception). In most case it is not preferable. We usually let the children own the relationship. This is done by using mappedBy

    (pseudo-code)

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

    The trick here is mappedBy

    0 讨论(0)
提交回复
热议问题