My Adventures in Coding

July 16, 2009

Hibernate – Cascade delete does not delete ALL related rows

Filed under: Hibernate — Brian @ 8:24 pm
Tags:

Recently I had a problem where a cascade delete was not deleting all related rows. I am fairly new to HIbernate, so I thought I would post my solution in case anyone else runs into this problem.

To explain the problem, lets say I have a hibernate entity called “Customer” that has a one to many mapping to another hibernate entity called “Subscriptions”. In my test I saved a Customer with several Subscriptions then deleted the Customer and asserted that all related entries in the Subscriptions table were removed. The problem was that each time only ONE entry was removed, but never ALL related entries in the Subscription table.

The “Customer” entity was set up with a relationship to “Subscriptions” as follows:

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @JoinColumn(name = "customerID", referencedColumnName = "id", nullable = false)
    private List subscriptions = new ArrayList();

The problem turned out to be that I needed to add a “Fetch” annotation:

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @JoinColumn(name = "customerID", referencedColumnName = "id", nullable = false)
    @Fetch(value = FetchMode.SUBSELECT)
    private List subscriptions = new ArrayList();

This solved the problem. Now ALL related rows in the Subscriptions table sub select are removed on cascade delete of a Customer.

Advertisements

3 Comments »

  1. You are awesome Sir.. I spent a week to fix but couldnt. Google should upvote your webpage

    Comment by Saurabh — March 21, 2014 @ 4:13 am | Reply

  2. Hi,

    I tried using your solution but it did not work for me.

    @OneToMany(fetch=FetchType.LAZY, mappedBy=”region”)
    @Cascade({CascadeType.DELETE})
    @Fetch(value = FetchMode.SUBSELECT)
    public Set getDistricts() {
    return this.districts;
    }

    Comment by hari — July 19, 2016 @ 7:48 am | Reply

  3. Hey I tried I like you but it did not work. can you please help me.

    @OneToMany(fetch = FetchType.EAGER,
    cascade = CascadeType.ALL,targetEntity=District.class,orphanRemoval = true,mappedBy=”region”)
    @Cascade( { org.hibernate.annotations.CascadeType.ALL } )
    @Fetch(value = FetchMode.SUBSELECT)
    public Set getDistricts() {
    return this.districts;
    }

    Comment by hari — July 20, 2016 @ 3:27 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: