Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 479401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:47:20+00:00 2026-05-13T00:47:20+00:00

Hibernate is not deleting orphans when I set the collection to null, although orphans

  • 0

Hibernate is not deleting orphans when I set the collection to null, although orphans are deleted when the collection is cleared. I have the following associations.

D - entity, contains a single embedded E
E - embedded object, contains one to many relationship with F (cascade type all,DELETE_ORPHAN)
F - entity, contains a collection of Strings

Note that because E is embedded in D, in the database E has no identity outside of D. Thus I will refer to D/E as a unit. Also notice that F contains a collection of Strings. Due to limitations in hibernate, this means that F must be an entity, not an embeddable value type. The cascade types of the collection of Fs in E include all and DELETE_ORPHAN.

If I want to remove the collection of Fs from D/E, I could explicitly clear all the Fs out of the collection, like this.

D d = //get a reference to a D
E e = d.getE();
Set<F> fs = e.getFs();
fs.clear();
//... update d in the session

This correctly removes all the database rows for Fs pertaining to D/E from the F table and the pertinent rows from the D/E to F join table, as I would expect. However, say I wanted to set the collection of Fs to null, like this.

D d = //get a reference to a D
E e = d.getE();
e.setFs(null);
//... update d in the session

Although this deletes the relationship between D/E and F in the join table, it does NOT remove the database rows for the Fs pertaining to D/E from the F table. Those database rows are now orphaned.

Is there a hibernate setting that allows one to set the collection of Fs to null and have hibernate realize that all Fs in that original collection are now orphaned?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:47:20+00:00Added an answer on May 13, 2026 at 12:47 am

    And how do you suppose Hibernate would know that it should delete all your F entities?

    Transitive persistence requires reachability; in order for Hibernate to know that all (or some) collection elements have been deleted it replaces your collection classes with its own that track those changes.

    You should never remove the reference to collection once created / instantiated; if you do the changes are lost (either silently behind the scenes or you may get an exception in some cases).

    Using clear() is the correct way to tell Hibernate you want collection elements deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Hibernate for ORM of my Java app to an Oracle database (not
I have a User and a set of Authorities in a one-to-many relationship: User.hbm.xml:
I stuck with entry deleting an entry from DB. I have Hibenrnate integrated with
Why does the UniqueConstraint annotation in the following Hibernate mapping declaration cause the exception
NHibernate is not really a good fit for our environment due to all the
Using NHibernate from C# and only HQL (not SQL) in a way that is
Assuming that writing nhibernate mapping files is not a big issue....or polluting your domain
I use NHibernate for my dataacess, and for awhile not I've been using SQLite
Hibernate has a handful of methods that, one way or another, takes your object
With Hibernate, can you create a composite ID where one of the columns you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.