在 NHibernate 命名查询中使用未映射的类 [英] Using Unmapped Class with NHibernate Named Query

查看:24
本文介绍了在 NHibernate 命名查询中使用未映射的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 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(String 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屋!

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