使用未映射类与NHibernate命名查询 [英] Using Unmapped Class with NHibernate Named Query
问题描述
我使用的是与NHibernate自定义命名查询,我要返回Person对象的集合。 Person对象未与NHibernate的映射,这意味着我得到了以下异常映射:
I'm using a custom named query with NHibernate which I want to return a collection of Person objects. The Person object is not mapped with an NHibernate mapping which means I'm getting the following exception:
System.Collections.Generic.KeyNotFoundException:
给定的关键是不存在于
字典。
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
在会话获取,因为创建它得到抛出当它调用NHibernate.Cfg.Mappings.GetClass(字符串的className),它不能找到类名。这一切都是很容易理解,但我想知道是否有任何方式告诉NHibernate的使用类,即使我没有为它映射?
It's getting thrown when the Session gets created because it can't find the class name when it calls NHibernate.Cfg.Mappings.GetClass(String className). This is all fairly understandable but I was wondering if there was any way to tell NHibernate to use the class even though I haven't got a mapping for it?
推荐答案
要解决这个问题,我结束了使用TupleToPropertyResultTransformer,并提供属性值的列表。有一些限制这一点,其中最主要的是,SQL查询必须返回的结果以相同的顺序为您提供性能的TupleToPropertyResultTransformer构造函数。
To solve this, I ended up using the TupleToPropertyResultTransformer and providing the list of property values. There are a few limitations to this, the main one being that the SQL query must return the results in the same order as you provide your properties to the TupleToPropertyResultTransformer constructor.
另外,属性类型推断,所以你必须要小心十进制列从使用TupleToPropertyResultTransformer提供给使用一个相当简单的方法只返回整数值等。除了SQL查询返回的对象的集合而不显式映射NHibernate的范围内的对象。
Also the property types are inferred so you need to be careful with decimal columns returning only integer values etc. Apart from that using the TupleToPropertyResultTransformer provided a reasonably easy way to use an SQL query to return a collection of objects without explicitly mapping the objects within NHibernate.
这篇关于使用未映射类与NHibernate命名查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!