本文介绍了如何在 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?

推荐答案

如果您使用延迟加载,Load 只会创建一个代理.

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,它不会尝试删除表中的数据B 当实际实体为 A 类型时.如果不加载实际对象,这是不可能的.当存在许多继承类型且每个类型又包含许多附加表时,这一点尤其重要.

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 有一个 Bs 的集合,其中包含 Cs(等等),它不会尝试当 Bs 的集合为空时,删除任何 Cs.这只有在阅读收藏时才有可能.当 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 和类型的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 23:56