如何在nhibernate中删除仅具有其ID和类型的实体? [英] How can one delete an entity in nhibernate having only its id and type?

查看:92
本文介绍了如何在nhibernate中删除仅具有其ID和类型的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用NHibernate 2.1删除仅具有其ID和类型的实体(如在映射中)?

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

推荐答案

如果您使用的是延迟加载,则加载"只会创建一个代理.

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

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

使用NH 2.1,您可以使用HQL.不确定其实际外观,但类似以下内容:请注意,这需要进行SQL注入-如果可能,请使用参数化查询代替SetParameter()

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));


对于Load,您不需要知道Id列的名称.

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

如果您需要了解它,可以通过NH元数据获取它:

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

sessionFactory.GetClassMetadata(type).IdentifierPropertyName


另一个编辑.


Another edit.

session.Delete()实例化实体

session.Delete() is instantiating the entity

使用session.Delete()时,NH仍会加载该实体.一开始我不喜欢它.然后我意识到了优势.如果实体是使用继承,集合或任何"引用的复杂结构的一部分,则实际上效率更高.

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.

例如,如果类AB都从Base继承,则当实际实体的类型为A时,它不会尝试删除表B中的数据.如果不加载实际对象,这将是不可能的.当存在很多继承类型并且每个继承类型都包含许多其他表时,这一点尤其重要.

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.

当您有一个Base的集合(碰巧是A的所有实例)时,也会出现相同的情况.将集合加载到内存中时,NH知道不需要删除任何B -stuff.

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.

如果实体A具有B s的集合,其中包含C s(依此类推),则当B s的集合时,它不会尝试删除任何C s.是空的.仅在阅读收藏集时才有可能.当C本身很复杂,聚集更多表等等时,这一点尤其重要.

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删除有陷阱

HQL删除以不将数据加载到内存中.但是删除HQL并不那么聪明.他们基本上将实体名称转换为相应的表名称,并将其从数据库中删除.此外,它会删除一些汇总的收集数据.

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".

结论

我还尝试使用NH优化删除.在大多数情况下,我都放弃了,因为NH仍然更聪明,它有效"并且通常足够快.我编写的最复杂的删除算法之一是分析NH映射定义并从中构建删除语句.而且-毫不奇怪-删除之前必须先从数据库中读取数据,这是不可能的. (我只是将其简化为仅加载主键.)

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.)

这篇关于如何在nhibernate中删除仅具有其ID和类型的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆