I am wondering how can one delete an entity having just its ID and type (as in mapping) using NHibernate 2.1?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
If you are using lazy loading, Load only creates a proxy.
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()
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:
Another edit.
session.Delete()is instantiating the entityWhen 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
AandBboth inherit fromBase, it doesn’t try to delete data in tableBwhen the actual entity is of typeA. 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 ofA. When loading the collection in memory, NH knows that it doesn’t need to remove anyB-stuff.If the entity
Ahas a collection ofBs, which containsCs (and so on), it doesn’t try to delete anyCs when the collection ofBs 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.)