Nhibernate - 执行 SQL 查询时出现异常
我正在使用 Nhibernate 执行 SQL 查询,下面是我用于此目的的代码:
public ArrayList getDocumentsForApproval(string ReleaseId)
{
string query = string.Format("SELECT distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101) as DateEntered FROM dbo.Documents doc INNER JOIN DevelopmentSteps ds ON doc.TypeId = ds.Id INNER JOIN DocumentTrackingItems dti ON doc.Id = dti.DocumentId INNER JOIN TrackingItems ti ON dti.ItemStepId = ti.Id INNER JOIN dbo.Releases rl ON ti.ReleaseId = rl.BugTrackerName left outer join (select * from users) as suser on doc.AuthorUserid = suser.Id WHERE doc.DateEntered IS NOT NULL AND doc.DateApproved IS NULL AND rl.ID = '{0}'", ReleaseId);
ISession session = NHibernateHelper.GetCurrentSession();
ArrayList document =(ArrayList) session.CreateSQLQuery(query).List();
return document;
}
我收到的错误信息如下:
**Exception Details:**
NHibernate.QueryException: Return types of SQL query were not specified [SELECT distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101)
可能是什么问题? - - 谢谢
I am executing a SQL Query using Nhibernate, below is the code in which I use for this:
public ArrayList getDocumentsForApproval(string ReleaseId)
{
string query = string.Format("SELECT distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101) as DateEntered FROM dbo.Documents doc INNER JOIN DevelopmentSteps ds ON doc.TypeId = ds.Id INNER JOIN DocumentTrackingItems dti ON doc.Id = dti.DocumentId INNER JOIN TrackingItems ti ON dti.ItemStepId = ti.Id INNER JOIN dbo.Releases rl ON ti.ReleaseId = rl.BugTrackerName left outer join (select * from users) as suser on doc.AuthorUserid = suser.Id WHERE doc.DateEntered IS NOT NULL AND doc.DateApproved IS NULL AND rl.ID = '{0}'", ReleaseId);
ISession session = NHibernateHelper.GetCurrentSession();
ArrayList document =(ArrayList) session.CreateSQLQuery(query).List();
return document;
}
The error information I receive is as follows:
**Exception Details:**
NHibernate.QueryException: Return types of SQL query were not specified [SELECT distinct doc.Id, doc.Name as Doc, doc.url as url, suser.Name as Author, ds.name, CONVERT(VARCHAR(11), doc.DateEntered, 101)
What could be the issue? ---- Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你从根本上误解了NHibernate。 NHibernate 与 TypeDataSource 类不同,它返回的数据集/数据表不是真正的业务对象。
NHibernate 旨在处理完全拥有的对象,因此您将拥有类似的内容
然后您需要手动或通过原始 HBM 映射的代码生成创建映射文件,或者使用 NH 之上的工具通过 FluentNHibernate 或 ConfORM 以编程方式构建映射。
在尝试查询之前,您需要学习 NHibernate 的基础知识,这是一篇不错的介绍性文章: http ://www.fincher.org/tips/Languages/NHibernate.shtml
然后可以使用http://www.castleproject.org/ActiveRecord/documentation/v1rc1/usersguide/hql.html 供参考。
You are fundamentally misunderstanding NHibernate. NHibernate is not like the TypeDataSource classes that return you DataSets/DataTables that aren't real business objects.
NHibernate is meant to work with fully owned objects so you would have something similar to
Then you need to create a mapping file either manually or by code generation for raw HBM mappings or use a tool on top of NH to build mappings programmatically with FluentNHibernate or ConfORM.
You need to learn the basics of NHibernate before attempting to query this is a decent introductory post: http://www.fincher.org/tips/Languages/NHibernate.shtml
And then for querying you can use http://www.castleproject.org/ActiveRecord/documentation/v1rc1/usersguide/hql.html for reference.
在大多数情况下,您应该使用实体对象而不是自定义查询。
如果您确实需要自定义查询,以下示例可能有用
In most cases you should use entity objects instead of custom queries.
If you really need a custom query, the following example might be useful
秘诀在于使用:
在
AddScalar
中,您必须定义输出的 NH 类型。请参阅此处
The secret is to use:
In
AddScalar
you have to define your NH types for output.See ref here