如何在 NHibernate 中获取部分对象?
我有一个带有 2 个字段的对象 O - A 和 B。如何从数据库中获取 O 以便仅获取字段 A?
当然,我的实际应用程序具有具有更多字段的对象,但是两个字段足以理解原理。
我正在使用 NHibernate 2.1。
谢谢。
编辑:
我想澄清一下。我需要获取 O 类型的对象。有时我想要获取完整的对象 - 这意味着 A 和 B 字段都是从数据库值设置的。但在其他情况下,我想仅使用数据库值中设置的 A 字段来获取 O 对象。
I have an object O with 2 fields - A and B. How can I fetch O from the database so that only the field A is fetched?
Of course, my real application has objects with many more fields, but two fields are enough to understand the principal.
I am using NHibernate 2.1.
Thanks.
EDIT:
I wish to clarify. I need to fetch objects of type O. Sometimes I will want to fetch complete objects - meaning both A and B fields are set from the database values. But on other occasions, I would like to fetch O objects with just the A field set from the database values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用投影将结果集缩小到所需的列,并使用结果转换器将结果转换为您想要的类型。
这将返回瞬态对象而不是持久实体。
Use a projection to narrow the result set to the desired column(s) and a result transformer to convert the result into the type that you want.
This will return transient objects rather than persistent entities.
HQL 有一个
select new
构造,它允许您仅获取字段的子集。但是,返回的对象无法保存回数据库。作为替代方案,您可以创建具有有限属性集的附加类,并将它们映射到相关表上。
HQL has a
select new
construct, which allows you to fetch only a subset of fields. Objects returned, however, cannot be saved back to the DB.As an alternative, you can create additional classes with a limited set of properties an map those onto table in question.