NHibernate 使用对象构造函数从 hql 命名查询返回重复行

发布于 2024-11-27 11:51:24 字数 438 浏览 1 评论 0原文

我有一个命名的 hql 查询,它使用未映射的对象的对象构造函数(仅导入),

例如

select distinct new NotMappedResultClass(ah.SomeProp1, ah.SomeProp2)
from SomeMappedClass
where ...
order by ah.SomeProp1

奇怪的是,当我在 NHibernate 中调用 IQuery.List() 时,我最终得到的结果正好是两倍来自 NHibernate 的行比来自 NHibernate 运行的查询(使用 SqlProfiler 跟踪)的行多。

(如果重要的话,“where”子句实际上涉及一些子查询)。

为什么 NHibernate 会复制从数据库返回的行?

(我使用的是 NHibernate 1.2.1.4000)

I have a named hql query which makes use of object constructors for an object that is not mapped (it is only imported)

e.g.

select distinct new NotMappedResultClass(ah.SomeProp1, ah.SomeProp2)
from SomeMappedClass
where ...
order by ah.SomeProp1

The weird thing is, that when I call IQuery.List() in NHibernate, I end up with exactly twice as many rows from NHibernate than from the query that NHibernate ran (traced using SqlProfiler).

(In case it matters, the "where" clause does actually involve some subqueries).

Why is NHibernate duplicating the rows coming back from the database?

(I am using NHibernate 1.2.1.4000)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2024-12-04 11:51:24

发现问题了。

当涉及继承类时,我的项目有一些奇怪的映射。

基本上,SomeMappedClass 是抽象的,并且有自己的 NHibernate 映射,并且有一个派生类 SomeDerivedClass(没有添加任何功能),它也是单独映射的,没有“extends”属性。

这导致 NHibernate 发出两个 SQL 查询,对同一个表使用不同的别名。

就我而言,简单快速但肮脏的解决方案是从 SomeDerivedClass 而不是 SomeMappedClass 进行查询,但更合适的解决方案可能是修改映射/对象继承。

Found the problem.

My project has some odd mappings when it comes to inherited classes.

Basically, SomeMappedClass was abstract, and had its own NHibernate mapping, and there was a derived class SomeDerivedClass (that didn't add any functionality) that was also mapped separately without the "extends" attribute.

This caused NHibernate to issue two sql queries, with different aliases for the same table.

In my case the simple quick and dirty solution was to query from SomeDerivedClass instead of SomeMappedClass, but the more appropriate solution would probably be to modify the mappings / object inheritance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文