NHibernate 使用对象构造函数从 hql 命名查询返回重复行
我有一个命名的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了。
当涉及继承类时,我的项目有一些奇怪的映射。
基本上,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.