将未映射的类与 NHibernate 命名查询一起使用
我正在使用 NHibernate 的自定义命名查询,我想返回 Person 对象的集合。 Person 对象未使用 NHibernate 映射进行映射,这意味着我收到以下异常:
System.Collections.Generic.KeyNotFoundException: 给定的密钥不存在于 字典。
创建 Session 时会抛出该异常,因为它在调用 NHibernate.Cfg.Mappings.GetClass(String className) 时找不到类名。 这都是可以理解的,但我想知道是否有任何方法告诉 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:
The given key was not present in the
dictionary.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不使用:
<代码>
query.SetResultTransformer(Transformers.AliasToBean(typeof(Person)));
它将使用列别名作为属性名称,将查询中每一列的数据插入到 Person 对象属性中。
Why don't you use:
query.SetResultTransformer(Transformers.AliasToBean(typeof(Person)));
It will insert data from each column in your query into Person object properties using column alias as a property name.
如何创建一个返回未映射类型实例的查询?
我认为米哈尔在这里说得有道理,也许你应该看看预测。 (至少,这就是我认为您正在寻找的)。
您可以在某些映射类型上创建查询,然后可以将该查询“投影”到“DTO”。
为了做到这一点,您必须“导入”您的 Person 类,以便 NHibernate 知道它,并且您必须使用 ResultTransformer。
像这样的事情:
但是,这仍然意味着您必须 导入该类,以便 NHibernate 知道它。
How can you create a query which would return instances of a type that is not mapped ?
I think Michal has a point here, and maybe you should have a look at projections. (At least, this is what I think you're looking for).
You create a query on some mapped type, and then, you can 'project' that query to a 'DTO'.
In order to do this, you'll have to 'import' your Person class, so that it is known to NHibernate, and you'll have to use a ResultTransformer.
Something like this:
But, this still means you'll have to import the class, so that NHibernate knows about it.
通过使用该类,NHibernate 基本上会猜测所涉及的所有内容,包括您打算用于 Person 的表以及字段映射。 NHibernate 可能会被黑客攻击,根据匹配名称或其他内容进行动态绑定,但整个想法是使用 xml 文件创建从普通旧数据对象到数据库字段的映射。
By using the class, NHibernate would basically be guessing about everything involved including which table you meant to use for Person, and the field mappings. NHibernate could probably be hacked to do dynamic binding based on matching the names or something, but the whole idea is to create the mappings from plain old data object to the database fields using the xml files.
如果没有充分的理由不映射类,只需添加映射即可获得最佳结果...
也就是说,您不能使用命名查询将结果直接注入到未映射的类中。 您需要告诉它哪些列放入哪些字段,或者换句话说,一个映射。 ;) 但是,您可以从命名查询返回标量值,并且可以获取这些对象数组并手动构建集合。
If there's not a really good reason not to map the class, simply adding the mapping will give you the best results...
That said, you can't use a named query to directly inject results into an unmapped class. You would need to tell it which columns to put into which fields or in other words, a mapping. ;) However, you can return scalar values from a named query and you could take those object arrays and build your collection manually.
为了解决这个问题,我最终使用 TupleToPropertyResultTransformer 并提供属性值列表。 对此有一些限制,主要的一个是 SQL 查询必须以与您向 TupleToPropertyResultTransformer 构造函数提供属性相同的顺序返回结果。
此外,属性类型也是推断出来的,因此您需要小心仅返回整数值的十进制列等。除此之外,使用 TupleToPropertyResultTransformer 还提供了一种相当简单的方法来使用 SQL 查询返回对象集合,而无需在 NHibernate 中显式映射对象。
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.
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.