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

  • SEARCH
  • Home
  • 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 323169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:00:23+00:00 2026-05-12T09:00:23+00:00

I am wondering how can one delete an entity having just its ID and

  • 0

I am wondering how can one delete an entity having just its ID and type (as in mapping) using NHibernate 2.1?

  • 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-12T09:00:23+00:00Added an answer on May 12, 2026 at 9:00 am

    If you are using lazy loading, Load only creates a proxy.

    session.Delete(session.Load(type, id));
    

    With NH 2.1 you can use HQL. Not sure how it actually looks like, but something like this: note that this is subject to SQL injection – if possible use parametrized queries instead with SetParameter()

    session.Delete(string.Format("from {0} where id = {1}", type, id));
    

    Edit:

    For Load, you don’t need to know the name of the Id column.

    If you need to know it, you can get it by the NH metadata:

    sessionFactory.GetClassMetadata(type).IdentifierPropertyName
    

    Another edit.

    session.Delete() is instantiating the entity

    When using session.Delete(), NH loads the entity anyway. At the beginning I didn’t like it. Then I realized the advantages. If the entity is part of a complex structure using inheritance, collections or “any”-references, it is actually more efficient.

    For instance, if class A and B both inherit from Base, it doesn’t try to delete data in table B when the actual entity is of type A. This wouldn’t be possible without loading the actual object. This is particularly important when there are many inherited types which also consist of many additional tables each.

    The same situation is given when you have a collection of Bases, which happen to be all instances of A. When loading the collection in memory, NH knows that it doesn’t need to remove any B-stuff.

    If the entity A has a collection of Bs, which contains Cs (and so on), it doesn’t try to delete any Cs when the collection of Bs is empty. This is only possible when reading the collection. This is particularly important when C is complex of its own, aggregating even more tables and so on.

    The more complex and dynamic the structure is, the more efficient is it to load actual data instead of “blindly” deleting it.

    HQL Deletes have pitfalls

    HQL deletes to not load data to memory. But HQL-deletes aren’t that smart. They basically translate the entity name to the corresponding table name and remove that from the database. Additionally, it deletes some aggregated collection data.

    In simple structures, this may work well and efficient. In complex structures, not everything is deleted, leading to constraint violations or “database memory leaks”.

    Conclusion

    I also tried to optimize deletion with NH. I gave up in most of the cases, because NH is still smarter, it “just works” and is usually fast enough. One of the most complex deletion algorithms I wrote is analyzing NH mapping definitions and building delete statements from that. And – no surprise – it is not possible without reading data from the database before deleting. (I just reduced it to only load primary keys.)

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

Sidebar

Related Questions

Hi I was wondering how can I call a method just for one time
One-to-one relations within nhibernate can be lazyloaded either false or proxy. I was wondering
I was wondering how one can fix an upper limit for the length of
i was wondering if any one can advise me on how i can go
I was wondering how can I programmatically copy all the discussion items from one
I'm wondering if anyone can recommend a good C++ tree implementation, hopefully one that
I keep wondering how does a debugger work? Particulary the one that can be
I'm wondering how can I submit a form via Ajax (using prototype framework) and
I was wondering how can I list the user tables name with its primary
I was wondering how can I delete the mouse over append when my mouse

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.