Hibernate ‘on-delete=cascade’ Performance Tuning

Be careful when you use Hibernate’s support for database ON DELETE CASCADE constraint. If not configured properly, your application might be more performance-costly than you think.

Let me use the example from Gavin King’s email introducing the new setting of on-delete=”cascade”:

<set name="children" inverse="true" cascade="all">
  <key name="PARENT_ID" on-delete="cascade">
  <one-to-many class="Child">
<set>

For a parent object associated with N child objects (cascade=”all”), the setting on-delete=”cascade” avoids issuing N deletes to the database if the parent were to be deleted.

Gavin also tells us that this setting only optimizes away the delete statements while all semantics are preserved. Well, what exactly does he mean?

I will roughly explain the procedure Hibernate performs to cascade delete in the simplest scenario:

  1. mark entity for deletion
  2. iterate through all of entity’s associations having cascade delete (one or many)
  3. if the association is single, perform delete on this association (by starting step 1 for this association)
  4. if the association is many, perform delete on this collection by:
    1. iterate all the collection elements, loading them from the database if necessary
    2. for each collection element, perform delete on the element (by starting step1 for this element)
  5. if not on-delete=”cascade”, issue delete statement on this entity

When you have a to-many association with cascade=”all” as described in our example, Hibernate iterates the associated collection having size N, loading them from database if necessary, and marks each collection element for deletion. Before actually issuing delete statements for these elements, Hibernate searches for any other cascades configured on them and perform these found cascading actions accordingly.

Even with the setting on-delete=”cascade” on a specific lazy collection, cascade=”all” causes Hibernate to initialize and iterate the collection (because it wants to check if the elements in this collection also cascade delete to other associations). Therefore, when you are sure there are no other cascading actions configured on your child entity (the element in the on-delete=”cascade” collection), use the setting cascade=”save-update” instead of cascade=”all” to prevent Hibernate from performing the delete cascade checks and consequently avoid loading your lazy to-many association into memory.

If you have multiple layers of delete cascade, from parent to children and from child to grand children, consider the size of your collections and decide if you want to set up multiple layers of ON DELETE CASCADE constraints in database accordingly or preserve the configuration of cascade=”all” and on-delete=”cascade” to let Hibernate handle the delete cascade on the grand children.

10 Responses to “Hibernate ‘on-delete=cascade’ Performance Tuning”

  1. Hi,
    I want to delete a record from parent table, so that it should also delete the corresponding record from child table,
    I need it in Hibernate Annotations,
    I specified as cascade(cascadeType.REMOVE)
    I am creating the table automatically

    can you please help me in this regard

    Thanks & Regds,
    Sridhar

  2. Hi,
    very informative…
    and it helped me a lot.
    thanks,
    Jeevan.

  3. Realy good and informative artical….

    Thanks a lot…..

    Sachin Misurkar …..

  4. venunath.K Says:

    Hi Tech brother,

    i am really thank full for this example cause i am learning hibernate myself for doing some project work. Its really help full for me.

    Eddie bro please keep the in this way only.

    GOD BLESS YOU to get success in all aspects.

    thanks a lot

  5. Markus Bohr Says:

    Hello,

    I still don’t understand why “on-delete” usage is restricted to inverse=”true”?
    The algorithm you listed above seems to be totally independent of the existence of an inverse i.e. many-to-one role…

  6. bestelkado-gadgethouse-1 Says:

    Hi! Someone in my Myspace group shared this site with us so I came to look it over. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers! Terrific blog and wonderful style and design.

  7. Durham Web Design…

    […]Hibernate ‘on-delete=cascade’ Performance Tuning « Eddie Hsiung’s Blog[…]…

  8. Hello! I understand this is sort of off-topic but I needed to ask.
    Does building a well-established blog like yours take a large amount of work?

    I am brand new to operating a blog however I do write in my diary on a daily basis.

    I’d like to start a blog so I can share my experience and feelings online. Please let me know if you have any kind of recommendations or tips for brand new aspiring blog owners. Appreciate it!

  9. you saved my day….thank you very much sir.

  10. I have fun with, cause I found just what I used to be looking for.nYou have ended my 4 day lengthy hunt! God Bless you man.nHave a great d Click https://twitter.com/moooker1

Leave a reply to Sunil Patidar Cancel reply